Goto command in matlab: Undefined function 'goto' for input arguments of type 'double'"
Show older comments
I am in need of using goto command in matlab. I came to know that there is no goto command in matlab. But there are programs that run using goto command (got from net) where i am getting the output.
Example 1
a = 5;
a = a - 1;
disp(a)
if a > 0
goto(4)
return
end
Example 2
a = 5;
if a == 5
goto(9)
return
else
goto(12)
return
end
disp('a equals five')
goto(13)
return
disp('a does not equal five')
For Example 1 and 2 i'm able to get the output. But when i am using in my program goto(170) i am getting an error "Undefined function 'goto' for input arguments of type 'double'".
Can somebody help me. Thanks in ADVANCE...!
9 Comments
the cyclist
on 25 Sep 2014
Can you at least tell us where "on the net" you got this function?
Have you read at all about why a goto function is not generally needed in a programming language?
jeffin
on 25 Sep 2014
jeffin
on 25 Sep 2014
"It goes without saying that this code is mainly just for entertainment purposes"
is the first line of the description for that file exchange submission.
There really is no programming problem for which goto statements could ever be considered the best solution even if they did exist in Matlab.
If you must press ahead and use that though then you should contact the author of the file exchange submission as I would be very surprised if any of the regular contributors to Matlab Answers are active users of that, unless they are of the type who desire "entertainment"
Putting all that aside, the most common reason for the error you are getting (other than the obvious one of the function simply not existing) is that the function is not on your Matlab path. Click the 'Set Path' option in the Home tab of the main Matlab window and add the folder containing the function file, remembering to click 'Save' afterwards.
Kelly Kearney
on 25 Sep 2014
It's probably a path issue.
But really, as others have stated, using goto statements is a terrible habit to get into, and I can't think of any code situation that would need it; even the author of the function you linked to states that the function is for entertainment purposes only. What are you actually trying to do in your program?
Do you type these examples in the command line, or are they run as scripts or functions? If they are functions perhaps they include a local function called goto , or include some path changing commands. Without seeing the full code, it is impossible to know.
But really the best solution is: do not use goto.
José-Luis
on 25 Sep 2014
I recently had to modify some FORTRAN code that used goto and I am still reeling from the experience. Spaghetti code is an apt name.
There are cases where its use is justified, for instance to escape from nested loops. Using a goto will make for much cleaner code than setting flags all over the place. However the goto construct the OP mentioned is not native Matlab.
Accepted Answer
More Answers (1)
Iain
on 25 Sep 2014
Ok, so you've downloaded a function "goto" from the file exchange, and it's probably just using "eval" on every line from the one you tell it, to the end of the function. (This is an orgy of bad programming practice.)
To use it, you need to make sure that the goto.m (or goto.p) file is on your matlab path:
addpath('C:\This is where I keep bad m files\')
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!