Use TIMER to call a function

13 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2019-7-28
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 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by