Can I ask Matlab to run from a previous line?

18 views (last 30 days)
I am running an if loop, and I would like my else condition to return me to line 3 in my code, which is the line just before this loop starts. Is it possible to do this? If not, how can I run a loop which continues to run until a certain condition is fulfilled?
  1 Comment
Star Strider
Star Strider on 12 Mar 2015
It would help if you posted or attached the relevant parts of your code.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 Mar 2015
If what you want to do is "is repeat the line before the loop and then go through 1 more iteration of the loop." then put the while loop inside a for loop
for k = 1 : 2
a=10; % Some "line before the loop"
while someCondition
% Code that you want to do... then....
b = 20; % Whatever code you want to do.
% Now, at the bottom of the loop, don't let it do
% more than 1 iteration if k == 2
if k >= 2
break;
end
end
end

More Answers (2)

the cyclist
the cyclist on 12 Mar 2015
I expect you want either the break or continue command.
  3 Comments
Eman Hussain
Eman Hussain on 30 Mar 2018
This is my problem in matlab I want solution of this problem

Sign in to comment.


Image Analyst
Image Analyst on 12 Mar 2015
If you want to do this manually within the debugger, it is not possible. With some languages, like Pharo, Lisp, C++ and Visual Basic, you can set a breakpoint and then grab the arrow and drag it to any other line you like and start executing from there. This is a very nice feature called "edit and continue" . You can change the code, then back up a line or two and re-execute that line of code with your new changes. Or if you stepped on a function and didn't like the outputs, then you could change the inputs to the function, say by typing in new values for them in the command window, and back up and run the function again. It would be truly awesome if MATLAB had that feature, but unfortunately MATLAB does not have that feature.

Categories

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

Community Treasure Hunt

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

Start Hunting!