MatLab Training Course Notes
Session 6

Creating Graphical User Interface (GUI) driven programs

Instructor:
Bill Miller
Dynamic Neuroimaging Laboratory
415.502.3726
http://dnl.ucsf.edu

Topics for Session 6

  1. Concepts: Interface, Responding to graphics events
  2. Structure of a GUI-program: a reliable approach
  3. GUIfft: An illustrative example
  4. Live response to mouse motion: two examples


1. Concepts: Interface, Responding to graphics events


2. Structure of a GUI-program: a reliable approach

Overall design approach:

This method of creating GUI programs has several advantages:

  1. Compactness: There is only one m-file, which gets called initially from the command line to create the interface and thereafter by the mouse-event callbacks of the graphics objects on the GUI.
  2. Readability: it is relatively easy to read and add functionality to the program, since different functionalities are separated into different sub-functions.
  3. CPU-economy: no continuous polling for events; the m-file is only executed when a callback event occurs, and exits when the appropriate response is completed.


3. GUIfft: An illustrative example

The following example, GUIfft.m illustrates the above style of making a GUI program. GUIfft has 3 functionalities:
  1. Open and load and display a signal from a text file. (only the last column of multi-column data is used).
  2. Compute the FFT (fast Fourier transform) of the signal and display it on the axis (replacing the original raw data).
  3. Display a horizontal threshold line over the raw data or FFT at a level specified by the user.

GUIfft's interface consists of a figure window containing:

To implement the above functionality and user interface, GUIfft.m contains a top-level function (GUIfft) and four subfunctions:

NameCalled byPurpose
GUIfft(action) User (command line) or graphics callbacks Decide which sub-function to call depending input argument
CreateGUI GUIfft when action='create' Creates GUI interface
LoadFile GUIfft when action='loadfile' Load and plot raw data
doFFT GUIfft when action='dofft' Compute and plot FFT of raw data
Threshold GUIfft when action='threshold' Create and place a threshold line on the axis

GUIfft.m includes extensive commenting to help you understand each line of each function.


4. Live response to mouse motion: two examples

In the above example program GUIfft, the "ButtonDownFcn" property of the axis used to generate a callback for creating a new threshold line when the mouse is clicked somewhere inside the axis. Holding the mouse button down while moving the mouse has no effect in this case. However, it is possible to obtain just such "live" movable behavior, by employing the following:

The following programs employ the above properties to create and manipulate movable lines on an axis (which can be used for thresholding or finding peaks, etc.):

The live action technique involves dynamically resetting the callback strings of the above properties, and is a bit beyond explaining here. Take a look at the help information provided with these functions, and the code itself to see how the behavior is acheived.