How do I use Goto in MATLAB

637 views (last 30 days)
Ahmed Yusef
Ahmed Yusef on 30 Jun 2015
Commented: Walter Roberson on 17 Nov 2023
How do I use Goto in MATLAB
  4 Comments
Michael Wengler
Michael Wengler on 17 Nov 2023
Sure, the purpose of a language is to teach you to be a "better" coder according to someone else's beliefs. To simply provide the tools that enable you to write the code you need would indeed be wrong.
Walter Roberson
Walter Roberson on 17 Nov 2023
It is computationally fairly expensive for optimizers to permit "go to". And it is not clear what should happen if a function did something like,
evalin('caller', 'goto Label7')

Sign in to comment.

Answers (5)

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.

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.

Walter Roberson
Walter Roberson on 30 Jun 2015

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

Zhunping Xue
Zhunping Xue on 5 Oct 2022
So I had run into a similar problem while trying to code a Turing machine simulation in matlab. Goto statements were really important because I was using a Post-Turing machine for simplicity, and goto statements are really fundamental to how such machines are built.
The way I got around it was using the eval function, and two tapes. The first tape would be the usual tape of symbols, the second tape is a tape of the program itself, all the instructions. This would be a cell array of strings. Then I can have a counter pointing to a cell in the instruction tape, and I'd just use eval to carry out the instruction in there. Goto statements simply become moving the counter around on the instruction tape -- move the counter to where the goto statement should be, then eval that statement.
I'm pretty sure this solution works generally. Instead of running the program like main.m, read that program into in a cell array of strings (main_cell), where each cell is simply one line in the program, then run the program like:
for i=1:length(main_cell)
eval(main_cell{i})
end
Then you can use goto statements by altering the index i in the for loop. Be careful of infinite loops, obviously :-P
  2 Comments
Stephen23
Stephen23 on 5 Oct 2022
You can avoid evil EVAL by storing function handles in the cell array, then simply call those function handles.
Zhunping Xue
Zhunping Xue on 7 Oct 2022
Hahahahaha... yes, I used one programming evil to solve another one. Your solution definitely works.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!