Timer Function for three commands or more
显示 更早的评论
Hi,
I want to use timer in m code in a way after certain time for example 10sec three different commands will be executed. For example:
tt = timer; tt.StartDelay = 10; tt.TimerFcn = @(~, ~)display('Hello') start(tt)
It will just do display('Hello') after 10 sec. In simple example, I want this code to do display('Hello') and display('dear') and display('James') all together.
P.S.: This is just a simple example. For sure, I do not want to use display in my code.
回答(1 个)
There are many ways. One is using the UserData:
tt = timer('StartDelay', 10, ...
'ExecutionMode', 'fixedRate', ...
'TimerFcn', @TimerCallback, ...
'UserData', 0);
start(tt)
And the callback:
function TimerCallback(TimerH, EventData)
Pool = {'Hello', 'dear', 'James'};
TimerH.UserData = TimerH.UserData + 1;
disp(Pool{TimerH.UserData});
if TimerH.UserData == 3
stop(TimerH);
end
end
Modern versions of Matlab contain a shot counter in the EvenData. But as far as I can see, the fields of EventData are not documented.
The 'TasksToExecute' will be useful for a stopping also.
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!