Graphics |
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.
Note If you are using the MATLAB Help browser, you can run this example or open it in the MATLAB editor. |
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.
% Initialize list box and make sure
% the hold toggle is set correctly
listBoxCallback(listBox,[]) holdToggleCallback(holdToggle,[])
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.
%% Callback for list box
function listBoxCallback(src,evt)% Load workspace vars into list box
vars = evalin('base','who'); set(src,'String',vars) end% listBoxCallback
Plot Button Callback
The plot button callback performs three tasks:
%% Callback for plot button
function plotButtonCallback(src,evt)% Get workspace variables
vars = get(listBox,'String'); var_index = get(listBox,'Value'); if length(var_index) ~= 2 errordlg('You must select two variables',... 'Incorrect Selection','modal') return end% Get data from base workspace
x = evalin('base',vars{var_index(1)}); y = evalin('base',vars{var_index(2)});% Get plotting command
selected_cmd = get(popUp,'Value');% Make the GUI axes current and create plot
axes(a) switch selected_cmd case 1% user selected plot
plot(x,y) case 2% user selected bar
bar(x,y) case 3% user selected stem
stem(x,y) end end% plotButtonCallback
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.
%% Callback for hold state toggle button
function holdToggleCallback(src,evt) button_state = get(src,'Value'); if button_state == get(src,'Max')% toggle button is depressed
hold(a,'on') set(src,'String','Hold On') elseif button_state == get(src,'Min')% toggle button is not depressed
hold(a,'off') set(src,'String','Hold Off') end end% holdToggleCallback
Why Use Function Handle Callbacks | Optimizing Graphics Performance |
© 1994-2005 The MathWorks, Inc.