Is there a way to execute multiple functions in Matlab at a specific time ???
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rejept Ivedic
am 5 Okt. 2018
Bearbeitet: Stephen23
am 6 Okt. 2018
For example I want to execute function one every 15 sec, function two every 40 sec and so on. I've tried to use timer object, but without any success.
function main
function one ();
disp('1')
end
function two ()
disp('2')
end
function three ();
disp('3')
end
end
0 Kommentare
Akzeptierte Antwort
Stephen23
am 5 Okt. 2018
Bearbeitet: Stephen23
am 5 Okt. 2018
For example, for your first function:
>> fun = @(~,~)disp('1');
>> t = timer('TimerFcn',fun, 'Period',15, 'ExecutionMode','fixedRate');
>> start(t)
... displays every fifteen seconds
>> stop(t)
>> delete(t)
Do the same for the other functions:
t1 = timer(...)
t2 = timer(...)
...
start(t1)
start(t2)
... whatever happens here
stop(t1)
stop(t2)
...
delete(t1)
delete(t2)
...
You could even put the timer objects into one array and use loops to perform those operations.
2 Kommentare
Stephen23
am 5 Okt. 2018
Bearbeitet: Stephen23
am 6 Okt. 2018
"I don't know where the second 1 comes from."
You told MATLAB to put them there.
"The results should be like:"
1
2
1
2
1
Nope. If you print a 1 every five seconds, and a 2 every ten seconds, and start the timers simultaneously (well, 1's first) then this is what we would expect:
1 (0 s)
2 (0 s)
1 (5 s)
1 (10 s)
2 (10 s)
1 (15 s)
1 (20 s)
2 (20 s)
1 (25 s)
...
Every five seconds a 1 gets printed, and every ten seconds a 2 gets printed, just like you told MATLAB to do. From your example it seems that you actually want both of the timers to have a 10 second period (not a five second period), and for the 2's to have a five second delay at the start. Try something like this:
>> t1 = timer('TimerFcn',@(~,~)disp('1'), 'Period',10, 'ExecutionMode','fixedRate');
>> t2 = timer('TimerFcn',@(~,~)disp('2'), 'Period',10, 'ExecutionMode','fixedRate', 'StartDelay',5);
>> start(t1), start(t2)
1
2
1
2
1
2
>> stop(t1), stop(t2)
These occur at these times:
1 (0 s)
2 (5 s - remember the start delay!)
1 (10 s)
2 (15 s)
1 (20 s)
2 (25 s)
...
so both of them clearly have a ten second period.
"so do I have to stop and delete t, t1 ... at the end of the program"
Yes, unless you want them to continue forever.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!