Inserting functions into timers
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Sean de Wolski
2011-6-10
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 个评论
Walter Roberson
2011-6-10
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!