Arash
Arash

Reputation:

Jump command in MATLAB

I'm working with the m-file editor of MATLAB and I need to jump from one line to another. If I need to jump from inside a For...end, I can't use the usual "while" technique.
Is there anyway to jump from a line to another, like goto in C?

Upvotes: 12

Views: 94081

Answers (4)

Seyhan ÖZEN
Seyhan ÖZEN

Reputation: 1

that solves the problem easily:

caseno = input('input your case no');

switch (caseno)
     case 1
          disp('this first section will run');
     case 2
          disp('this second section will run');
     otherwise
          disp('wrong case no');
end

Upvotes: 0

gnovice
gnovice

Reputation: 125864

There is no goto statement in MATLAB, but there are a few other commands for use with loops that may help you:

  • continue: This statement will skip the remaining commands in a for or while loop and move on to the next iteration.
  • break: This statement will terminate execution of a for or while loop.

Upvotes: 20

Prince Rajput
Prince Rajput

Reputation: 1

for j = 1: 1: 24
  % LABEL start
a = a + j;
if a > 10
goto('start') % If condition satisfied goto label start
return
else
  a = a + 1;
end
end

Upvotes: -1

user85109
user85109

Reputation:

There is no goto in matlab. However, this does not say you cannot structure your code to use an {if, else, elseif, end} structural form for your code. Or use a {switch, case, end} form. Or call a function (or subfunction or nested function) to solve your problem. Or use a continue/break to structure your code. One can always solve these problems using one of the existing forms of flow control available.

The use of functions can improve your code in other ways, often by making it more modular, and therefore easier to debug and write.

Upvotes: 9

Related Questions