Use TIMER to call a function

13 visualizzazioni (ultimi 30 giorni)
Pablo De Jarmy
Pablo De Jarmy il 28 Lug 2019
Risposto: Walter Roberson il 28 Lug 2019
I have a function called watch which accepts clock() as input. I would like this function to run every second, so I created a timer sec to call that function
function watch(time)
disp(time)
end
sec = timer;
set(sec,'ExecutionMode','fixedRate','TimerFcn',{@watch,clock()});
start(sec)
But the code produces the following error:
Error while evaluating TimerFcn for timer 'timer-14'
Too many input arguments.
What is the correct way of calling the watch function? Using R2017b
Thanks.

Risposta accettata

Walter Roberson
Walter Roberson il 28 Lug 2019
function watch(~, ~, time)
Note that this will always give you the same time. When you create a callback such as [@watch,clock()} then those arguments are evaluated at the time the callback is constructed, so the time of callback construction is what is going to be recorded in the callback.
You should consider,
sec = timer;
set(sec,'ExecutionMode', 'fixedRate', 'TimerFcn', @(varargin) disp(datetime()));
start(sec)

Più risposte (0)

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Tag

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by