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'?
Is this for debugging, or a functional part of your algorithm?
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).
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?
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
Great job! You just solve my question.
Cool. Glad it helped.

请先登录,再进行评论。

更多回答(1 个)

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).
both of them work,
True. I didn't read it carefully, and thought you wrote "... until you press return".

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by