필터 지우기
필터 지우기

How can I execute two functions with a time delay between the two calls?

조회 수: 16 (최근 30일)
I have the following script:
[G_d,G_o, isolated,active,phi] = Single_CF_initial(G,50,1,0.2);
for tt = 2:25
if isolated(tt-1)- isolated(tt)==0
break
end
[G_d, isolated,Needremove] = Single_CF_Stages(G_d,phi,isolated,active);
[G_r,isolated] = H(G_d, G_o,0.95,isolated);
G_d = G_r;
end
I want to call the function "H" after some time of calling both functions "Single_CF_initial" and "Single_CF_Stages". Let us say after 15 seconds.
Is it possible to accomplish this in MATLAB?
Thanks!

채택된 답변

Adam Danz
Adam Danz 2021년 6월 8일
If you want execution to stay "busy" during the 15 sec wait time, use a simple tic/toc evaluation. It's not clear which of the two functions are invoked second or if that's subject to change, but the second one should set the start time using ts=tic; and then enter a while-loop until toc(ts)>=15 after which the H function can be called (I'm hoping that's just a paceholder for the actual function name). Note that the wait period could be placed at the end of the second function or outside of the functions after the second one is called.
If you want Matlab's busy state to be 'ready' during the 15 sec wait time, a much more efficient method would be to use a timer. The timer can be created any time before the functions are called and will have a start delay of 15 sec. After the second function is called you can merely start the timer and it will execute its callback function after 15 sec. See Timer Callback Functions. See this answer for an example of using a timer that sets a variable value 3 minutes after an app is started.
If you get stuck, share your updated code and let us know where you are at and what the problem is.
  댓글 수: 21

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

추가 답변 (1개)

Gatech AE
Gatech AE 2021년 6월 8일
편집: Gatech AE 2021년 6월 8일
If you use the "pause" function, you can specify a time in seconds as the input. It does require integers and uses decimals as fractional seconds.
[G_d,G_o, isolated,active,phi] = Single_CF_initial(G,50,1,0.2);
waitTime = 15;
for tt = 2:25
if isolated(tt-1)- isolated(tt)==0
break
end
[G_d, isolated,Needremove] = Single_CF_Stages(G_d,phi,isolated,active);
pause(waitTime)
[G_r,isolated] = H(G_d, G_o,0.95,isolated);
G_d = G_r;
end
If you don't know how long you'll actually need to pause in advance, using "input" is another option that pauses execution until an input is provided.
  댓글 수: 1
Waseem AL Aqqad
Waseem AL Aqqad 2021년 6월 10일
Hi Gatech,
Thanks for your reply.
The pause function didn't work out. I got the same exact plot.

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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by