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.

 Accepted Answer

Brian B
Brian B on 3 Mar 2013
Edited: Brian B on 3 Mar 2013

0 votes

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 Comments

QiQin Zhan
QiQin Zhan on 3 Mar 2013
Edited: QiQin Zhan on 3 Mar 2013
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?
QiQin Zhan
QiQin Zhan on 3 Mar 2013
Edited: QiQin Zhan on 3 Mar 2013
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 on 3 Mar 2013
Edited: Brian B on 3 Mar 2013
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 on 3 Mar 2013
Edited: QiQin Zhan on 3 Mar 2013
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 on 3 Mar 2013
Edited: Brian B on 3 Mar 2013
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.

Sign in to comment.

More Answers (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 Comments

Brian B
Brian B on 3 Mar 2013
Edited: Brian B on 3 Mar 2013
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".

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!