Is there a way to execute multiple functions in Matlab at a specific time ???

조회 수: 2 (최근 30일)
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

채택된 답변

Stephen23
Stephen23 2018년 10월 5일
편집: Stephen23 2018년 10월 5일
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
Rejept Ivedic
Rejept Ivedic 2018년 10월 5일
Thank you for the quick answer Stephen Cobeldick! The code works better but the results are not satisfied. My goal is to create a program which allows to call any programs at a specific period, so do I have to stop and delete t, t1 ... at the end of the program. I've tried this code:
function main
fun1 = @(~,~)disp('1');
fun2 = @(~,~)disp('2');
t1 = timer('TimerFcn',fun1, 'Period',5, 'ExecutionMode','fixedRate');
t2 = timer('TimerFcn',fun2, 'Period',10, 'ExecutionMode','fixedRate');
start(t1)
start(t2)
end
and the results are :
1
2
1
1
2
1
1
2
1
1
I don't know where the second 1 comes from. The results should be like:
1
2
1
2
1
2
Stephen23
Stephen23 2018년 10월 5일
편집: Stephen23 2018년 10월 6일
"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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 10월 5일
Use timer objects.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by