Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

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:

  1. an array of line numbers
  2. 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.

20 Responses to “Vectorized goto in MATLAB”

  1. Doug Schwarz replied on :

    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

  2. Steve H replied on :

    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!

  3. Urs (us) Schwarz replied on :

    …what’s todays date…
    us

  4. martin cohen replied on :

    You can’t possibly put this into Matlab without also including the well-known “come-from” statament.

  5. John D'Errico replied on :

    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

  6. Markus replied on :

    Hehe, nice April fool!

  7. David Schwartz replied on :

    I think it should distinguish between “Quiet NaNs” and “Signaling NaNs”; that way, you can go quietly or kicking and screaming.

  8. BA.Solymosi replied on :

    I believe a goto(myself) would converge to the answer “42″.

  9. A. Cohn replied on :

    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.

  10. M'hamedi replied on :

    les simulation avec matlab/simulink

  11. Tim Davis replied on :

    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.

  12. Tom Clark replied on :

    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.

  13. huda replied on :

    hello,
    please I need example for goto in matlab.
    and how can use Exit statment in matlab?
    thanks

  14. huda replied on :

    hello,
    please i need example for goto statment in matlab.
    and how can use the exit statment in matlab?

    thanks

  15. Loren replied on :

    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

  16. huda replied on :

    Ok ,What I can use instead of goto I need it such this command in my code?

    THANKS,HUDA

  17. Loren replied on :

    Huda-

    Look into try-catch, and writing functions that are reusable so you can call them from different sites in your code.

    –Loren

  18. Dr. Phillip M. Feldman replied on :

    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.

  19. Mray replied on :

    Often, I like to ‘break’ out of nested loops.
    It would really help having a function that would allow that.

  20. Loren replied on :

    Dr. Phillip Feldman and Mray,

    I have placed your request in the enhancement database. Thank you for the feedback.

    –Loren

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Loren: Hannah- If you are looking to see if a value of a field is set, use one of the string comparison functions,...
  • Hannah: Hi, Is there any way to check for the existence of structure elements? For example… >> c c =...
  • John DErrico: Hi Pam, Interpolation in a higher number of dimensions is interesting for a few reasons. In fact, I...
  • Loren: Michel- I am not sure you can extend it. What you can do is run it twice, first with 3 points, and then, if...
  • Michel Charette: My head’s still spinning trying to figure out how the solution got from ultra-fragile floating...
  • Loren: Thanks Yi. Also, for what it’s worth, I have put in an enhancement request to have the deletion syntax...
  • Yi Cao: Loren, It is an interesting experiment. I wish to point out that even in the situation where a set of...
  • Pam: Dear John, The mini lecture is great! I am interested in knowing how to linearly interpolate for higher...
  • Loren: Jebb- Good question! In today’s MATLAB you don’t need to do more than A(1) = B(1) but I was trying...
  • jebb: I was interested to see this method of forcing a non-lazy copy: A = B; A(1) = A(1) + sin(0); Presumably the...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics