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'.

 채택된 답변

Jan
Jan 2012년 11월 15일

4 개 추천

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

Rahman
Rahman 2012년 11월 15일
Thanks Jan Simon. It worked.
Walter Roberson
Walter Roberson 2012년 11월 15일
Not quite. N1 should also be invoked after N is. The label N has no goto or return or other flow modification, so after N is done, flow should continue on to N1. After N1 is done (either path), flow would "fall off the end of the script" and would not reach the "return" that are shown in the code after the goto()
Jan
Jan 2012년 11월 16일
Correctly, Walter. I've been confused by the "return" statements in the original code, which cannot be reached. Therefore I assumed, that this is a demo code only to illustrate the question. As answer I demonstrated how to use functions instead of GOTOs. While the codes are not equivalent, they demonstrate the different ideas.
K Vineeth Reddy
K Vineeth Reddy 2016년 5월 12일
편집: Walter Roberson 2016년 5월 12일
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 2016년 5월 12일
Please show your code.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Ilham Hardy
Ilham Hardy 2012년 11월 15일

1 개 추천

There is no
goto
function in matlab.
The only
goto

댓글 수: 3

Stephen23
Stephen23 2016년 7월 12일
편집: Stephen23 2016년 7월 12일
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.
Claudio
Claudio 2024년 6월 14일
as much as people thinks that "goto" is a beginner statement, it helps keeping down complexity and write cleaner code in certain scenarios, tha lack of it is appalling because it forces much more complex patterns where there is absolutely no need for.
It is as appalling as the lack of a finally in the try catch blocks
Steven Lord
Steven Lord 2024년 6월 14일
tha lack of it is appalling because it forces much more complex patterns where there is absolutely no need for.
What examples of such scenarios do you have in mind? It's possible that there's a more efficient way to do what you're thinking about than the implementation you've thought of.
Just in my personal opinion:
Are there cases where having a goto statement in MATLAB could make certain operations easier? Likely, yes. But not, IMO, to the point of being "appalling". To my mind "appalling" is a pretty high bar and not having a feature that has been so hotly debated over multiple decades doesn't clear that bar.
Are there cases where code would be much, much more complicated if MATLAB had a goto statement? Likely, yes. Search MATLAB Answers for "eval" if you want to see how people can make things much more complicated by using a tool when it may not be necessary.
Does the benefit of introducing a goto statement in MATLAB outweight the potential cost (to MathWorks, to design / implement / test / document that statement, and to users who may misuse it to write convoluted code)? That's much less clear. [And no, "just do what other language X does" is not a design for the feature. MATLAB is not C. MATLAB is not C++. MATLAB is not Java, Fortran, Python, or any other language.]
It is as appalling as the lack of a finally in the try catch blocks
That seems like a reasonable enhancement request you could send to Technical Support directly using this link.

댓글을 달려면 로그인하십시오.

Shruthi rajagopalan
Shruthi rajagopalan 2013년 4월 6일

0 개 추천

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

Walter Roberson
Walter Roberson 2013년 4월 6일
Could you demonstrate the structure you would use to write it in "goto" form?
Howard Lam
Howard Lam 2016년 6월 27일
편집: Howard Lam 2016년 6월 27일
for i = 1 : N
statements;
for j = 1 : M
statements;
if x > y
goto here;
end
end
statements;
here:
end
I have something like this. Can you help please?
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.
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

질문:

2012년 11월 15일

댓글:

2024년 6월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by