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.

类别

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