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
  2 Comments
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
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
G=0;
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
disp('Flor')
G=0;
case 2
disp('Circulo')
G=0;
case 3
disp('Onda senoidal')
G=0;
case 4
disp('Elipse')
G=0;
case 5
disp('Nube')
G=0;
case 0
disp('Fin del programa')
G=G+1;
otherwise
disp('Inserte otro valor')
end
end

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
...
end
If it's an error condition you're running into, you can use try...catch instead.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!