MATLAB Answers

Stop and Continue Iterations

87 views (last 30 days)
Kiran Sagar
Kiran Sagar on 8 Jul 2016
Commented: Steven Lord on 10 Jul 2016
I am running a loop as follows:
tol = 1e-6;
while err < tol
% code
disp(tol)
end
During runtime, I decide to stop and check something, and continue the running the program again. I can do this using Ctrl+C, but the program terminates inside the loop, which is undesirable. Is it possible to stop the iterations and continue again in some other way.

  3 Comments

Geoff Hayes
Geoff Hayes on 8 Jul 2016
Kiran - what do you stop to check and how would you continue your processing in the same manner or change some variables? Please provide more details.
Brendan Hamm
Brendan Hamm on 8 Jul 2016
As of version MATLAB 2016a you can pause the execution of a running file. prior to this you would need to place a breakpoint if you want it to stop inside the loop. If you know the condition on which you want it to stop you can place a conditional breakpoint.
Kiran Sagar
Kiran Sagar on 10 Jul 2016
@Geoff, I wouldn't be changing the variables, I will plot a few graphs to check if the progress is correct.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 8 Jul 2016
Click the pause button on the tool ribbon, next to the Run button. It looks like the pause button on media players, like two vertical bars.

  3 Comments

Sean de Wolski
Sean de Wolski on 8 Jul 2016
Note, you will have to run the script or function in order to use the pause button (new in 16a). It does not work if you run the section using the run section buttons.
Image Analyst
Image Analyst on 10 Jul 2016
Kiran, one hack if you don't want to use the pause button and you're in a loop is to just put an if in the loop to stop at some particular iteration you know in advance
if k == 42
; % Set a breakpoint on this line
end
but the preferred way is to just put a conditional breakpoint on the line. Click the down arrow on the Breakpoints button and select Set Condition. Then type k == 42 into the condition edit field.
With the pause button, you don't know what iteration you will be in when you stop, but with the conditional breakpoint, you will.
Steven Lord
Steven Lord on 10 Jul 2016
Instead of leaving that middle line empty except for a semicolon, call keyboard on it.

Sign in to comment.

Sign in to answer this question.