Effacer les filtres
Effacer les filtres

Inserting functions into timers

2 vues (au cours des 30 derniers jours)
Morten
Morten le 10 Juin 2011
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?

Réponses (1)

Sean de Wolski
Sean de Wolski le 10 Juin 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 commentaires
Walter Roberson
Walter Roberson le 10 Juin 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.
Sean de Wolski
Sean de Wolski le 10 Juin 2011
Walter, make that a new answer and I'll delete this one.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by