How to use 'goto' statement?

87 Ansichten (letzte 30 Tage)
Rahman
Rahman am 15 Nov. 2012
Bearbeitet: Stephen23 am 12 Jul. 2016
I have the problem in the code segment:
if matchedRulesCount==0
goto('N')
return
else
goto('N1')
return
end
fprintf('Number of rules matched are:%d\n',matchedRulesCount);
%LABEL N
disp('No rule is fired.');
%LABEL N1
choice=input('Do yo want to try once more? \n0. For No.\n1. For yes. \n');
I get the following error
??? Undefined function or method 'goto' for input arguments of type 'char'.

Akzeptierte Antwort

Jan
Jan am 15 Nov. 2012
if matchedRulesCount==0
N;
else
choice = N1;
end
return;
function N
disp('No rule is fired.');
function choice = N1
choice=input('Do yo want to try once more? \n0. For No.\n1. For yes. \n');
It is a very good idea to avoid goto. It is even not a part of Matlab!
  5 Kommentare
K Vineeth Reddy
K Vineeth Reddy am 12 Mai 2016
Bearbeitet: Walter Roberson am 12 Mai 2016
this is not working in my machine.
"A nested function cannot be defined inside a control statement (if, while, for, switch or try/catch).
Be aware that in addition to this message, other messages might appear in the file. Such as, <control statement> might not be aligned with its matching END and Parse error at END: usage might be invalid MATLAB syntax. When you remove the nested function definition from the control statement, these other messages might disappear"
I am getting error like this.
Walter Roberson
Walter Roberson am 12 Mai 2016
Please show your code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Ilham Hardy
Ilham Hardy am 15 Nov. 2012
There is no
goto
function in matlab.
The only
goto
  1 Kommentar
Stephen23
Stephen23 am 12 Jul. 2016
Bearbeitet: Stephen23 am 12 Jul. 2016
Note that that FEX submission introduces itself with this line: "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."
Beginners who find this page and want to use goto should learn how to use functions or other more robust programming syntaxes. This will make their lives much easier.

Melden Sie sich an, um zu kommentieren.


Shruthi rajagopalan
Shruthi rajagopalan am 6 Apr. 2013
Hi, Can anyone please help me out with the below query: I want to use a goto statement inside if-else statement.How should i do that? I came across in mathworks that there is no goto in matlab.Only Continue and break is available for For loop and while loop alone. How should i jump to another statement when I am using If-Else Statement? Thanks! Shruthi
  4 Kommentare
Kenneth Lamury
Kenneth Lamury am 12 Jul. 2016
According to user-defined function (script) http://www.mathworks.ch/matlabcentral/fileexchange/26949-matlab-goto-statement, you might be using it wrong. Try the following:
for i = 1 : N
statements;
for j = 1 : M
statements;
if x > y
goto(here);
return %must have a return after the goto function
end
end
statements;
% LABEL here
end
Remember that it's goto('pointer','file') usage; 'file' is optional.
Walter Roberson
Walter Roberson am 12 Jul. 2016
Without using the user contributed fake goto, you would use
for i = 1 : N
statements;
goto_here = false;
for j = 1 : M
statements;
if x > y
goto_here = true;
break;
end
end
if ~goto_here
statements;
end
%here:
end

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by