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 Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2013년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by