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

14 ビュー (過去 30 日間)
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 6 月 8 日
編集済み: Waseem AL Aqqad 2021 年 6 月 12 日
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 件のコメント
Adam Danz
Adam Danz 2021 年 6 月 12 日
High five 🖐🏼
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 6 月 12 日
High five 🖐 You’re the best.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by