Effacer les filtres
Effacer les filtres

How do I use Goto in MATLAB

436 vues (au cours des 30 derniers jours)
Ahmed Yusef
Ahmed Yusef le 30 Juin 2015
Commenté : Walter Roberson le 17 Nov 2023
How do I use Goto in MATLAB
  4 commentaires
Michael Wengler
Michael Wengler le 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 le 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')

Connectez-vous pour commenter.

Réponses (5)

Thorsten
Thorsten le 30 Juin 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 le 29 Juin 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 le 30 Juin 2015

Christopher Avila
Christopher Avila le 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 le 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 commentaires
Stephen23
Stephen23 le 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 le 7 Oct 2022
Hahahahaha... yes, I used one programming evil to solve another one. Your solution definitely works.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by