Enumerating MATLAB Command Iterations

I wasn’t trying to come up with the most boring sounding title for a post, but I just couldn’t stray from the descriptivist banality of what is about to unfold.  Well, banal only to some.  Clearly not to me or Nick Higham, whose interest in mathematical peculiarities extends beyond the numerical and functional to the editorial as well.

In a recent post, he raises a curious point that certain MATLAB commands can be applied to themselves.  This is curious because the functions may or may not accept numerical or string inputs, but MATLAB’s weak-typing allows us to interpret the string input as whatever is required, in some circumstances.

For example,

>> diff diff

returns the differences between subsequent array elements, and returns

ans =
    5    -3    0

by interpreting the input as the ASCII numeric value of the string input.

Most of the trickery is of this flavor, casting string input as a numeric array and then going on our merry way.  What’s cutest is Nick Higham’s line of thinking: what about triple (and naturally, more) invocations of these functions?  How many are valid?  How many are interesting?

MATLAB has helped us pose the question and it is perfectly equipped to answer it.  With 20 lines of code to recurse the folder tree of the default MATLAB path and 10 more to run the commands we’re interested in, I have found some interesting results.  Mind you, I’m stuck in the past (2012b) so your mileage may vary.

gtext gtext

is surely the first most interesting thing, since it’s the first double-invocation that requires user interaction.  Yes, there are plenty of dialog boxes and figures opened before this while-looping through all double-invocations alphabetically, but this is the first worth mentioning.  A click in the figure let’s us continue.

input input

is the next one that stops us in our tracks, but we can’t actually interact with the console until we close a GUI Options dialog that’s been opened by the following.

guideopts guideopts

Close that window, enter our ‘input’ into input and we’re back on our way…after we click through the our first required dialog box.

inputdlg inputdlg

The next group is interesting because they stop us at breakpoints–quite unexpected surprises in that regard!

javaaddpath javaaddpath
javaclasspath javaclasspath
javarmpath javarmpath
publish publish

Also,

pack pack
questdlg questdlg
tsnewevent tsnewevent
uigetdir uigetdir
uigetfile uigetfile
uiopen uiopen
uiputfile uiputfile
uisetcolor uisetcolor
uisetfont uisetfont

required a click somewhere, and then we’re home free until we start over again looking at triple-invocations and more break points in

dbstop dbstop dbstop
inputdlg inputdlg inputdlg

and the Java path tools.  My favorite is coming up, which will continue to work for all number of invocations:

menu menu menu

from which we have to select a valid menu item from the “menu” menu, which in this case will be ‘menu’.

A question dialog will get us yet again, and forever it seems, as will tsnewevent and the ui functions along with some others that only work with an even number of inputs.  But, after closing enough dialog boxes and continuing through enough break points, at 10 invocations we’ve converged a bit on behavior and can look at some population results.

First, there are some funny ones based on their console output that should be included as honorable mentions:

ndgrid
numel
str2mat
strcat
strvcat
tsParseBufferStr
type
vertcat

But what we really want to look at is how many functions can be called how many times.  And also, how many functions have their maximum-allowable amount of invocations at any given count. 

invocations

So what are these cool functions that have a finite limit to their valid invocation count and aren’t already talked about here?  Highlights are:

cov cov cov
dot dot dot
kron kron kron
(l|ml|mr|r)divide (l|ml|mr|r)ldivide(l|ml|mr|r)ldivide
union union union
sparse sparse sparse sparse
spline spline spline spline
pde pde pde pde pde
polyval polyval polyval polyval polyval

and the rest appear to be arg checking, dialog boxes, and those that you can keep invoking forever.  I’m sure there are others in the list that I haven’t identified as interesting, so you should look at the list here and try running the code yourself in some later version of MATLAB.

OH!  I almost forgot to include the list of dangerous functions.  These either combinatorially exploded, corrupted the MATLAB path, or even caused a segfault in later function execution!

cell
depdir
depfun
matlabpath
memmap_data_handle_holder
path
spinmap
rjr

Well, there you have it.  An non-exhaustive description of an exhaustive enumeration of MATLAB functions and their various methods of self-invoking.  Thanks, Nick Higham, for the inspiration!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: