Jump command in MATLAB

南楼画角 提交于 2019-11-26 22:26:12

问题


I'm working with the m-file editor of MATLAB and I need to jump from one line to another. If I need to jump from inside a For...end, I can't use the usual "while" technique.
Is there anyway to jump from a line to another, like goto in C?


回答1:


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.



回答2:


There is no goto in matlab. However, this does not say you cannot structure your code to use an {if, else, elseif, end} structural form for your code. Or use a {switch, case, end} form. Or call a function (or subfunction or nested function) to solve your problem. Or use a continue/break to structure your code. One can always solve these problems using one of the existing forms of flow control available.

The use of functions can improve your code in other ways, often by making it more modular, and therefore easier to debug and write.




回答3:


that solves the problem easily:

caseno = input('input your case no');

switch (caseno)
     case 1
          disp('this first section will run');
     case 2
          disp('this second section will run');
     otherwise
          disp('wrong case no');
end



回答4:


for j = 1: 1: 24
  % LABEL start
a = a + j;
if a > 10
goto('start') % If condition satisfied goto label start
return
else
  a = a + 1;
end
end


来源:https://stackoverflow.com/questions/1082605/jump-command-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!