How do I use Goto in MATLAB

447 次查看(过去 30 天)
Ahmed Yusef
Ahmed Yusef 2015-6-30
How do I use Goto in MATLAB
  4 个评论
Michael Wengler
Michael Wengler 2023-11-17
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 2023-11-17
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')

请先登录,再进行评论。

回答(5 个)

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

Walter Roberson
Walter Roberson 2015-6-30

James Van Zandt
James Van Zandt 2021-6-29
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.

Christopher Avila
Christopher Avila 2019-11-23
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 2022-10-5
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 个评论
Stephen23
Stephen23 2022-10-5
You can avoid evil EVAL by storing function handles in the cell array, then simply call those function handles.
Zhunping Xue
Zhunping Xue 2022-10-7
Hahahahaha... yes, I used one programming evil to solve another one. Your solution definitely works.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by