Loren on the Art of MATLAB

Turn ideas into MATLAB

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.

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.