views:

712

answers:

3
+2  Q: 

Jump in m-file

I'm working with the m-file editor of MATLAB and I need to jump from a line to another. As long as 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?

Best Regards

+2  A: 

As far as I know, there is no goto statement in Matlab. But you can almost always avoid goto by restructuring your program.

danatel
+9  A: 

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.
gnovice
+5  A: 

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.

woodchips

related questions