Graphics Previous page   Next Page

Example -- Using Function Handles in GUIs

This example creates a simple GUI that plots workspace variables. It is defined in a single M-file that contains both the layout commands and the callbacks. This example uses function handles to specify callback functions. Callbacks are implemented as nested functions to reduce the need to pass variables as arguments.

See Function Handle Callbacks for more information on the use of function handle callbacks.

Complete Example Code

The documentation for this example does not list all the code used to lay out and program the GUI. To see a complete code listing, use the links in the note box below.

The GUI Layout

The following picture shows the GUI after running the example code. The program creates two variables (testvarX and testVarY) in the base workspace for testing purposes.

The GUI layout is split among three uipanel containers. One contains the axes, the right side panel contains a list box to display workspace variables, and the bottom panel contains the plot and hold buttons and the plot type pop-up menu.

Initialize the GUI

The list box and the hold toggle button need to be initialized before the GUI is ready to use. This is accomplished by executing their callbacks. Note that because you are calling these functions directly, MATLAB does not implicitly pass the first two arguments, as it would if these functions were executed as callbacks in response to an event. You therefore must explicitly pass all arguments in these function calls.

The Callback Functions

TheGUI components that have callbacks are the list box, toggle button, and plot push button. In addition, the figure's three uipanels define resize functions that MATLAB executes whenever users resize the figure.

See Programming the Resize Functions for information on writing callback functions for the figure and uipanel ResizeFcn properties.

List Box Callback

The list box callback generates a list of the current variables in the base workspace using the evalin and who functions. It then assigns this list to the list box String property so that it displays these variable names.

Note how the function takes advantage of the fact that the first argument passed to the callback is the handle of the callback object (i.e., the source of the callback event, which is the list box). Therefore, whenever you click in the list box, MATLAB updates the list to display the current workspace variables.

Plot Button Callback

The plot button callback performs three tasks:

Hold State Toggle Button Callback

The toggle button callback requires the handles of the GUI figure and axes. Because these callbacks are written as nested functions, the figure handle (f) and the axes handle (a) are in scope within the callback.

You want the GUI to toggle the hold state, but the GUI figure handle is hidden. It is necessary, therefore, to use the axes handle as the first argument to the hold function.

Previous page  Why Use Function Handle Callbacks Optimizing Graphics Performance Next page

© 1994-2005 The MathWorks, Inc.