Loren on the Art of MATLAB
April 1st, 2006
Vectorized goto in MATLAB
Customers have long asked for a GOTO statement in MATLAB to allow them to use programming constructs familiar to them from other languages they already know. I am pleased to announce that a proposal is making its way through the design process at MathWorks and I thought I'd use this opportunity to also get your feedback. Search the MATLAB newsgroup to find many discussions on the topic of GOTO.
goto statements can be invaluable in certain types of programs. In fact, we do use it occasionally in the MATLAB C source code ourselves. To quote a sage, “The problem with GOTOs is not the GOTO itself, but with the label.” So, we propose to introduce the goto statement into MATLAB, with the usual MATLAB unique twist. Instead of labels identifying where to go, users can supply the destination(s) one of two ways:
- an array of line numbers
- a cell array of strings, possibly with regular expressions, to denote the next lines of code containing the given expressions
Upon reaching a goto, MATLAB goes to each line "simultaneously" and proceeds from each location.
The rules for goto are scoped to the file of interest,. We still need to specify the rules.
Behavior for line numbers:
- Inf : go directly to the end of the program without any further execution (think of it like end)
- NaN : go to random places in the program and try to continue (Note: you might get errors because you might end up starting to evaluate something starting in the middle of a statement.)
- complex values: branch the goto so real parts of all data go to the real part of the number, and imaginary parts go to the imaginary portion of the number
- logical : if true continue as if the goto was not there at all; if false, follow rules for Inf
- non-integral numeric values : go to the line specified by the integer part of the number, and calculate the starting location in the line using the fractional portion of the number
-
- non-positive numbers : round up to 1, meaning go back to the beginning of the file
Behavior for cell arrays of strings:
- found strings : go to the first line in the file with the string. If a particular string is repeated go to the second/third etc. instance and wrap back around to the first instance if you can't find more
- for strings that aren't present in the program : see behavior for NaN
- no string matches found at all : see behavior for NaN
Recent reactions to this proposal include this from Mike Karr:
This spec fills a much needed gap.
What do you think? Put your comments here.
For more insight into other new features expected in MATLAB and other products, you might also be interested in Steve Eddins' blog post this week as well.
07:00 UTC |
Posted in Uncategorized |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
Loren,
This is great, but I think the behavior for NaN is counter-intuitive. To get a random line you should just use some variation on the rand function. I think NaN should go to a different function altogether.
Doug
Fantastic! I can’t tell you how long I’ve needed ‘goto(NaN)’. My work around was to load the file contents in a var, check its length, pick a random position, and ‘eval’ that part of the code. Now all that will be one simple command!
…what’s todays date…
us
You can’t possibly put this into Matlab without also including the well-known “come-from” statament.
I like the goto(-inf) variant. It reboots your computer. Also, if the target line is a BeenThere statement, then it should cancel the goto.
John
Hehe, nice April fool!
I think it should distinguish between “Quiet NaNs” and “Signaling NaNs”; that way, you can go quietly or kicking and screaming.
I believe a goto(myself) would converge to the answer “42″.
You had me going for a minute there… Vectorized goto! I was hopeful of finally having a multi-threaded application. Well, I hear it is coming soon anyway, after the new Class System is implemented.
les simulation avec matlab/simulink
I think this article is missing a few features.
Combining goto’s and function pointers would be very useful.
goto(@f) would jump to the function f without all the messy syntax of input/output arguments. This would allow the function f to modify its inputs, a sorely needed MATLAB feature.
goto(@f(2:5)) would do the same thing, but would execute only lines 2 to 5 of the function f.
Then of course there would be the command
goto(blas)
which would ditch the standard BLAS and use Goto’s BLAS instead. That’s surely the best Goto going. Being able to switch BLAS at run time would be great, rather than all that messy
setenv BLAS_VERSION /usr/lib/libgoto.so
setenv LAPACK_VERBOSITY
to which we need to add
setenv GOTO_VERBOSITY
which would report every statement a goto goes to. Perhaps the latter could be achieved if you left off the semicolon, so that
goto(42) ;
is silent, but
goto(42)
would report “Going to line 42 to find the answer to the question of life, the universe, and things in general.”
And finally, we can’t forget goto’s and the symbolic toolbox. goto(’solution’) would skip all that messy algorithmic stuff, O(n^3)-time x=A\b when A is dense, and so on. It would just skip ahead and immediately goto the solution. Optimization would be specified by goto(’max(@f)’), or if you want to minimize your solution, just use goto(’min(@f)’).
I suppose goto([]) would be a no-op?
Now, if can only come up with a specification for a sparse goto … what would it mean if you do goto(list) and “list” is a sparse vector? MATLAB has to do everything the same for both sparse and full, after all.
No more for now, I gotta get my sparse goto going.
Today being April 1 + 0.5 (Sept 1), after all, this is only half as good as a true April 1st spec.
It seems I’m far far too gullible.
Maybe I should goto a psychiatrist about it, although in my defence, it’s now the 3rd of August.
hello,
please I need example for goto in matlab.
and how can use Exit statment in matlab?
thanks
hello,
please i need example for goto statment in matlab.
and how can use the exit statment in matlab?
thanks
Huda-
There is no GOTO statement in MATLAB. This was a joke on April Fool’s Day. The command in MATLAB for exit is simply exit. Perhaps you’d benefit from reading the “Getting Started with MATLAB” guide.
–Loren
Ok ,What I can use instead of goto I need it such this command in my code?
THANKS,HUDA
Huda-
Look into try-catch, and writing functions that are reusable so you can call them from different sites in your code.
–Loren
Turning serious for just a moment: In nested loops, “break” exits only from the loop in which it occurs. If Matlab had a goto statement, one legitimate application would be to break out of nested loops. Currently, there is no good way of handling this situation in Matlab.
Often, I like to ‘break’ out of nested loops.
It would really help having a function that would allow that.
Dr. Phillip Feldman and Mray,
I have placed your request in the enhancement database. Thank you for the feedback.
–Loren
Hello
I try to figure out what is going with “go to” statment in MatLab but I am afraid it’s a big mess for me.My issue is the simplest one: Let’s say I am in line 32 and I want to go in line 87 how I do that?
What happens when I am in an IF statment and I need to say:
if(bla bla bla)
matlab code
else
“go to” line 51
end
Thanks in advance
Nick
Nick-
It really depends on your code. You can rearrange code to use subfunctions or nested functions, and break and continue statements in loops.
–Loren