MATLAB Answers

How to pause the code?

100 views (last 30 days)
QiQin Zhan
QiQin Zhan on 3 Mar 2013
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.

  0 Comments

Sign in to comment.

Accepted Answer

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

Show 6 older comments
Brian B
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
QiQin Zhan
QiQin Zhan on 3 Mar 2013
Great job! You just solve my question.
Brian B
Brian B on 3 Mar 2013
Cool. Glad it helped.

Sign in to comment.

More Answers (1)

Youssef  Khmou
Youssef Khmou on 3 Mar 2013
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
Actually it lets you execute statements and examine variables until you press F5 (not return).
Youssef  Khmou
Youssef Khmou on 3 Mar 2013
both of them work,
Brian B
Brian B on 3 Mar 2013
True. I didn't read it carefully, and thought you wrote "... until you press return".

Sign in to comment.

Tags