Use TIMER to call a function

13 ビュー (過去 30 日間)
Pablo De Jarmy
Pablo De Jarmy 2019 年 7 月 28 日
回答済み: Walter Roberson 2019 年 7 月 28 日
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 Center および File ExchangeEntering Commands についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by