MATLAB: Create Callback Function in GUI

callback functionguiguide

I'm using GUIDE to create a GUI with a slider. I want to be able to move the slider and it to update the plot on the GUI. I think I need to use a callback function but I am not sure how to do this. My code is below. In the code I want the variable 'theta' to update with the value of the slider and I then want the new theta value to be put in the next coding section where it will run a rotation matrix. To work sucessfully I want the plot to be continuously updated each time I click on the slider.
function varargout = testing123(varargin)
% TESTING123 MATLAB code for testing123.fig
% TESTING123, by itself, creates a new TESTING123 or raises the existing
% singleton*.
%




% H = TESTING123 returns the handle to a new TESTING123 or the handle to
% the existing singleton*.
%
% TESTING123('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTING123.M with the given input arguments.
%
% TESTING123('Property','Value',...) creates a new TESTING123 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before testing123_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to testing123_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help testing123
% Last Modified by GUIDE v2.5 16-Sep-2013 13:03:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @testing123_OpeningFcn, ...
'gui_OutputFcn', @testing123_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before testing123 is made visible.
function testing123_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB




% handles structure with handles and user data (see GUIDATA)


% varargin command line arguments to testing123 (see VARARGIN)
% Choose default command line output for testing123
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes testing123 wait for user response (see UIRESUME)
%uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = testing123_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global theta
global x
global y
global x_rotated
global y_rotated
global x_center
global y_center
theta= guidata(handles.slider1)
x=[1; 2; 3; 4];
c=[4; 5; 67; 6];
y = c;
%close all
% create a matrix of these points, which will be useful in future calculations
v = [x y];
% choose a point which will be the center of rotation
x_center = x(1);
y_center = y(1);
% create a matrix which will be used later in calculations
center = repmat([x_center; y_center], 1, length(x));
% define a 60 degree counter-clockwise rotation matrix
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
% do the rotation...
%s = v - center; % shift points in the plane so that the center of rotation is at the origin
%so = R*s; % apply the rotation about the origin
%vo = so + center; % shift again so the origin goes back to the desired center of rotation
% this can be done in one line as:
vo = R*((v - center')') + center;
% pick out the vectors of rotated x- and y-data
x_rotated = vo(1,:);
y_rotated = vo(2,:);
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
%global theta
%theta = get(handles.slider1,'Value')
% hObject handle to slider1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.

function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1
global x
global y
global x_rotated
global y_rotated
global x_center
global y_center
% make a plot
subplot(2,1,1)
plot(x, y, 'k-', x_rotated, y_rotated, 'r-', x_center, y_center, 'bo');
axis equal
subplot(2,1,2)
plot(x_rotated,y_rotated)
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

Best Answer

You had it right but then commented it out:
function slider1_Callback(hObject, eventdata, handles)
%global theta
%theta = get(handles.slider1,'Value')
so all you have to do now is to use theta in whatever way to do to plot the stuff you want to plot. But it looks like you put that code in the output_fcn for some reason. This will run on startup and on shutdown. But what you should do is to put all that stuff in a standalone function called "PlotMyData" or whatever. That function should take handles as an input, and be called from your slider callback, and from either the opening_fcn() or the output_fcn() function, but not both. Output_fcn gets run after opening_fcn() gets run. Sometimes it's necessary to put things in the output function if what you're doing depends on the GUI being displayed. Sometimes you can set properties for controls in the Opening function but if that's not working, then put it in the output function and it should work then.