How to pause the code?

We can use 'Pause'function to pause what's running(e.g in a for loop).If we want to continue,we can just press the keyboard.Then my question is whether there is a way or another similar function which we can set a flag to pause or to continue.

 採用された回答

Brian B
Brian B 2013 年 3 月 3 日
編集済み: Brian B 2013 年 3 月 3 日

0 投票

If you only want to pause when specific conditions are satisfied, you can put the pause or keyboard statement inside an if clause.
Another option is to use conditional breakpoints. See http://blogs.mathworks.com/community/2010/08/09/debugging-points/.

9 件のコメント

QiQin Zhan
QiQin Zhan 2013 年 3 月 3 日
編集済み: QiQin Zhan 2013 年 3 月 3 日
In my code,I want to continue when specified conditions are satisfied.Is it possible to set a flag to do it?Or is there an another similar function like 'pause'?
Brian B
Brian B 2013 年 3 月 3 日
Is this for debugging, or a functional part of your algorithm?
QiQin Zhan
QiQin Zhan 2013 年 3 月 3 日
編集済み: QiQin Zhan 2013 年 3 月 3 日
It is a part of my GUI algorithm.I have 'pause' the code using a buttom,and when I exit the GUI.I must first press on the keyboard.
pause;
try
body
catch
return
end
Otherwise,it may not return.
Brian B
Brian B 2013 年 3 月 3 日
編集済み: Brian B 2013 年 3 月 3 日
Do you mean that you want to pause to wait for a condition to become true?
If you execute pause, the whole program stops execution. Thus, unless you are waiting for an external trigger of some sort, any conditions that are true when the pause is first executed ought to be true until the program resumes (after you press a key).
Brian B
Brian B 2013 年 3 月 3 日
So you want program execution to continue after the GUI is closed? Are there other conditions on which you want to continue?
QiQin Zhan
QiQin Zhan 2013 年 3 月 3 日
編集済み: QiQin Zhan 2013 年 3 月 3 日
Is it possible that when I press another buttom,the condition becomes true and the program resumes?Or is it possible to set a flag to solve it?
Brian B
Brian B 2013 年 3 月 3 日
編集済み: Brian B 2013 年 3 月 3 日
One way to accomplish this, if you have a loop that you want to pause/continue, is to use the buttons to set a variable to record the state (play/pause). Then, in the loop, just check the value of that variable.
hgui = mygui(); % gui init
while 1
gd = guidata(hgui);
while guidata.state == 0 % paused
pause(0.1);
gd = guidata(hgui);
end
% rest of loop
end
QiQin Zhan
QiQin Zhan 2013 年 3 月 3 日
Great job! You just solve my question.
Brian B
Brian B 2013 年 3 月 3 日
Cool. Glad it helped.

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

その他の回答 (1 件)

Youssef  Khmou
Youssef Khmou 2013 年 3 月 3 日

0 投票

hi Chan,
you can use the function " keyboard" :
doc keyboard;
When you insert the function in function/script , it stops until you type return .

3 件のコメント

Brian B
Brian B 2013 年 3 月 3 日
編集済み: Brian B 2013 年 3 月 3 日
Actually it lets you execute statements and examine variables until you press F5 (not return).
Youssef  Khmou
Youssef Khmou 2013 年 3 月 3 日
both of them work,
Brian B
Brian B 2013 年 3 月 3 日
True. I didn't read it carefully, and thought you wrote "... until you press return".

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by