Timer Function for three commands or more
Show older comments
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.
Answers (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.
Categories
Find more on Structures 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!