Inserting functions into timers

조회 수: 2 (최근 30일)
Morten
Morten 2011년 6월 10일
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?

답변 (1개)

Sean de Wolski
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
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.
Sean de Wolski
Sean de Wolski 2011년 6월 10일
Walter, make that a new answer and I'll delete this one.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by