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.

 Akzeptierte Antwort

Brian B
Brian B am 3 Mär. 2013
Bearbeitet: Brian B am 3 Mär. 2013

0 Stimmen

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 Kommentare

QiQin Zhan
QiQin Zhan am 3 Mär. 2013
Bearbeitet: QiQin Zhan am 3 Mär. 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'?
Brian B
Brian B am 3 Mär. 2013
Is this for debugging, or a functional part of your algorithm?
QiQin Zhan
QiQin Zhan am 3 Mär. 2013
Bearbeitet: QiQin Zhan am 3 Mär. 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 am 3 Mär. 2013
Bearbeitet: Brian B am 3 Mär. 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).
Brian B
Brian B am 3 Mär. 2013
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 am 3 Mär. 2013
Bearbeitet: QiQin Zhan am 3 Mär. 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 am 3 Mär. 2013
Bearbeitet: Brian B am 3 Mär. 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
QiQin Zhan
QiQin Zhan am 3 Mär. 2013
Great job! You just solve my question.
Brian B
Brian B am 3 Mär. 2013
Cool. Glad it helped.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Youssef  Khmou
Youssef Khmou am 3 Mär. 2013

0 Stimmen

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 Kommentare

Brian B
Brian B am 3 Mär. 2013
Bearbeitet: Brian B am 3 Mär. 2013
Actually it lets you execute statements and examine variables until you press F5 (not return).
Youssef  Khmou
Youssef Khmou am 3 Mär. 2013
both of them work,
Brian B
Brian B am 3 Mär. 2013
True. I didn't read it carefully, and thought you wrote "... until you press return".

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by