It goes without saying that this code is mainly just for entertainment purposes, since using goto() is considered lazy programming and is a debugging nightmare. With that out of the way, I present to you goto.m, similar to the goto() or jump() commands in other programming disciplines.
Syntax:
goto(pointer,file)
return % All goto() commands need to be followed by a 'return'!
pointer could be an integer representing a line number in the m file, a string addressing a label in the m file, an array consisting of line numbers to consecutively jump to, or a cell array consisting of label strings to jump to. Using inf as an input jumps to the end of the code, while using NaN jumps to a randomly chosen line number (credit to Loren for the not-so-deliberate inspiration).
The second input, file, is optional. It states the code file to which the we want to jump to (default is current m file).
Twenty four example files are accompanied in the zip folder. Check the usage of this function inside them in order to understand how to implement goto() in your own routines. An example is shown below.
Example 23:
labels = {'add';'init1';'add';'add';'show'};
goto(labels)
return
% LABEL init1
a = 0;
goto('End')
return
% LABEL add
a = a + 1;
goto('End')
return
% LABEL init2
a = 0;
goto('End')
return
% LABEL show
msgbox(num2str(a))
% LABEL End
引用格式
Husam Aldahiyat (2024). MATLAB Goto Statement (https://www.mathworks.com/matlabcentral/fileexchange/26949-matlab-goto-statement), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
平台兼容性
Windows macOS Linux标签
致谢
启发作品: LINEAR AND CIRCULAR CONVOLUTION, demonstrate the linear convolution process for DTS and LTS
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.8.0.0 | Changed explanation and help. |
||
1.7.0.0 | Added ability to jump to a different m file. |
||
1.6.0.0 | Added string label support as well as vector/cell input support. |
||
1.5.0.0 | Added support for jumping to a label string. |
||
1.3.0.0 | Edited some comments. |
||
1.2.0.0 | Closed the open fids. |
||
1.1.0.0 | Fixed a lot of bugs, made code much more fool-proof, added support for continue and break, added five more examples. |
||
1.0.0.0 |