Inserting functions into timers
1 view (last 30 days)
Show older comments
I'm trying to use a timer to periodically read data from a text file (which is being changed by a different program elsewhere), and then plot the results in a set of axes that is on a GUI.
I've read the various examples for how to use timers, but I don't understand how I'm to execute a function using TimerFcn.
At the moment, I have this callback function which sort of works..
function pushon_Callback(hObject, eventdata, handles)
t = timer('StartDelay', 0, 'Period', 2, 'TasksToExecute', 5, ...
'ExecutionMode', 'fixedRate');
t.TimerFcn = { @my_callback_fcn, 'bloop'};
axes(handles.axes1);
start(t)
function my_callback_fcn(obj, event, string_arg)
plot(rand(5));
axes(handles.axes1);
hold on;
Except it errors out when I'm trying to specify the axes I'm using
??? Error while evaluating TimerFcn for timer 'timer-7'
Undefined variable "handles" or class "handles.axes1".
If I remove it, then the first plot is done in the right box but subsequent ones come up in a popped out figure.
How do I do this?
0 Comments
Answers (1)
Sean de Wolski
on 10 Jun 2011
handles isn't passed into the timer function so it can't see it.
t.TimerFcn = {@my_callback_fcn, 'bloop',handles};
and
function my_callback_fcn(obj, event, string_arg,handles)
2 Comments
Walter Roberson
on 10 Jun 2011
Caution: instead of passing in the handles structure to the timer, you should pass in the figure handle, and then use guidata() in the timer in order to obtain the current version of the handles structure.
If you pass in the handles structure, you get a copy of it as it existed at the time the timer was created, and that copy will not update as changes are made.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!