Use TIMER to call a function
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pablo De Jarmy
am 28 Jul. 2019
Beantwortet: Walter Roberson
am 28 Jul. 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.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 28 Jul. 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)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!