필터 지우기
필터 지우기

How do I use Goto in MATLAB

조회 수: 462 (최근 30일)
Ahmed Yusef
Ahmed Yusef 2015년 6월 30일
댓글: Walter Roberson 2023년 11월 17일
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.

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.

Walter Roberson
Walter Roberson 2015년 6월 30일

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에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by