MATLAB Answers

How do I use Goto in MATLAB

1,117 views (last 30 days)
Ahmed Yusef
Ahmed Yusef on 30 Jun 2015
Answered: James Van Zandt on 29 Jun 2021
How do I use Goto in MATLAB
John D'Errico
John D'Errico on 30 Jun 2015
... you need it because you know how to write code in some other language, and don't want to learn MATLAB, and the tools available. Maybe because someone gave you some existing code in some other language, and you want to convert it directly to MATLAB, often without really understanding that code, or the target language.
Instead, learn about while loops. Learn about functions, and how to write or use them. Learn about if/end blocks, about the continue and break statements.

Sign in to comment.

Answers (4)

Thorsten on 30 Jun 2015
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.

Christopher Avila
Christopher Avila on 23 Nov 2019
u can´t use goto on matlab XD
maybe u can use while like this
while G==0
disp('Seleccione una figura')
disp('1 = Flor')
disp('2 = Circulo')
disp('3 = Onda senoidal')
disp('4 = Elipse')
disp('5 = Nube')
disp('0 = Cerrar Programa')
P = input(' ');
switch P
case 1
case 2
case 3
disp('Onda senoidal')
case 4
case 5
case 0
disp('Fin del programa')
disp('Inserte otro valor')

James Van Zandt
James Van Zandt on 29 Jun 2021
There's no way to jump into a block of code. However, if you need to jump out of, say, several nested IF tests, you can put the whole thing in a FOR loop and jump out with a BREAK statement:
for i =1
if <something awful> break; end
If it's an error condition you're running into, you can use try...catch instead.


No tags entered yet.

Community Treasure Hunt

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

Start Hunting!