MATLAB Goto Statement

버전 1.8.0.0 (9.21 KB) 작성자: Husam Aldahiyat
The one and only.
다운로드 수: 6.6K
업데이트 날짜: 2011/2/17

라이선스 보기

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 (2026). MATLAB Goto Statement (https://kr.mathworks.com/matlabcentral/fileexchange/26949-matlab-goto-statement), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2007b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Just for fun에 대해 자세히 알아보기

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