Writing apps

There is no great magic about apps in Matlab.  They just use ordinary Matlab code.


When you create variables, they are visible in the command window (type “who” or “whos”).  There is though a separate (invisible) set of variables - these are special numbers (a bit like file names) that identify every graphic object.  Each figure, axes, line or surface has such a “handle” and any change to its properties causes it to be automatically redrawn.  Try for instance:

   get(gca)

(“gca” stand for “get current axes handle”) and

   set(gca, ‘color’,’b’)


In addition to the obvious graph objects, you can create uimenus and uicontrols.  Try (use single quotes, not speech marks):

  h = uicontrol(‘style’,’push’, ‘string’, ‘Don’’t touch me’,…

         ‘callback’,’disp(‘’Ouch!’’)’)

The callback is simply a string that gets executed when you click on it.  All you need to do then is write the code to be used when each button gets clicked.  To avoid it getting really messy we use just one function (with a variety of input arguments to identify the button that was pushed) to deal with all the possible options.

It makes life a lot easier if, whenever we create an object, we keep a record of its handle.   Ideally we don’t want this list to be a variable in the command window; someone might delete it and it gets messy if you have more than one window.   So what we do is collect all the handles into a “structure” variable and set that as the “userdata” (a special property, essentially a private filing space) belonging to some obvious graphics object such as the figure window.  Try

    a.myName = ‘Who?’;

  a.myID = 314159;

  a.aMatrix = rand(10,4);

  set(gca, ‘userdata’, a)

  b = get(gca, ‘userdata’)

Remember that “help” and “doc” will give you help on any topic, e.g. “help uicontrol”.


© Roger Moss 2015