Use TIMER to call a function
6 views (last 30 days)
Show older comments
Pablo De Jarmy
on 28 Jul 2019
Answered: Walter Roberson
on 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 Comments
Accepted Answer
Walter Roberson
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!