MATLAB: Running handles from GUI using timer object

guihandlestimer

Hello,
I am a beginner to MATLAB, tried searching on the help docs and Google, but wasn't able to figure out why my timer object won't run a particular GUI handle. I generated the GUI using GUIDE. The problem I have, in brief is following: I can call my function 'rando' from a button callback, but it doesn't seem to work if I call it from a timer object.
If you could please help me with this, I'd appreciate it. Thanks in advance. Here's the code:
function varargout = trial(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @trial_OpeningFcn, ...
'gui_OutputFcn', @trial_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 trial is made visible.
function trial_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 trial (see VARARGIN)
% Choose default command line output for trial
handles.output = hObject;
% [X, map] = imread('C:\Users\Lemonickous\Documents\Courses\DC\DCseminar\images, etc\dc\untitled.jpg');
% image(X)
% colormap(map)
% axis off
handles.a = timer;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes trial wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = trial_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)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%trial2;
delete(handles.figure1);
% --- Executes on button press in generate.
function generate_Callback(hObject, eventdata, handles)
% hObject handle to generate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t2 = randi(10);
set(handles.a, 'ExecutionMode', 'fixedrate');
set(handles.a, 'Period', .1);
set(handles.a, 'TasksToExecute', t2);
set(handles.a, 'TimerFcn', 'rando(hObject, eventdata, handles)');
start(handles.a)
% --- Executes on button press in button2.
function button2_Callback(hObject, eventdata, handles)
% hObject handle to button2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rando(hObject, eventdata, handles);
function rando(hObject, eventdata, handles)
t = randi(3);
if t == 1
out = 200000;
elseif t == 2
out = 240000;
else
out = 280000;
end
set(handles.disp, 'String', num2str(out));

Best Answer

Any callback coded as a string is executed within the context of the base workspace. The base workspace does not have a rando function defined in it, as rando is private to the file trial.m
You should instead pass a function handle.
Do it like this:
function generate_Callback(hObject, eventdata, handles)
% hObject handle to generate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

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

t2 = randi(10);
set(handles.a, 'ExecutionMode', 'fixedrate');
set(handles.a, 'Period', .1);
set(handles.a, 'TasksToExecute', t2);
set(handles.a, 'TimerFcn', {@rando, hObject});
start(handles.a)
% --- Executes on button press in button2.
function button2_Callback(hObject, eventdata, handles)
% hObject handle to button2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rando(hObject);
function rando(hObject)
t = randi(3);
if t == 1
out = 200000;
elseif t == 2
out = 240000;
else
out = 280000;
end
handles = guidata(hObject);
set(handles.disp, 'String', num2str(out));