Accessing Workspace Variables from a List Box

Workspace Variable Example Outcome

This GUI uses a list box to display workspace variables, which the user can then plot.

Techniques Used in This Example

The following figure illustrates the layout.

simple plotter GUI

Note that the list box callback is not used in this program because the plotting actions are initiated by push buttons. In this situation you must do one of the following:

View Completed Layout and Its GUI M-File

If you are reading this in the MATLAB® Help browser, you can click the following links to display the GUIDE Layout Editor and the MATLAB Editor with a completed version of this example. This enables you to see the values of all component properties and to understand how the components are assembled to create the GUI. You can also see a complete listing of the code that is discussed in the following sections.

Reading Workspace Variables

When the GUI initializes, it needs to query the workspace variables and set the list box String property to display these variable names. Adding the following subfunction to the GUI M-file accomplishes this using evalin to execute the who command in the base workspace. The who command returns a cell array of strings, which are used to populate the list box.

function update_listbox(handles)
vars = evalin('base','who');
set(handles.listbox1,'String',vars)

The function's input argument is the handles structure generated by the GUI M-file. This structure contains the handle of the list box, as well as the handles all other components in the GUI.

The callback for the Update Listbox push button also calls update_listbox.

Reading the Selections from the List Box

This GUI requires the user to select two variables from the workspace and then choose one of three plot commands to create the graph: plot, semilogx, or semilogy.

Enabling Multiple Selection

To enable multiple selection in a list box, you must set the Min and Max properties so that Max - Min > 1. This requires you to change the default Min and Max values of 0 and 1 to meet these conditions. Use the Property Inspector to set these properties on the list box.

How Users Select Multiple Items

List box multiple selection follows the standard for most systems:

Users must use one of these techniques to select the two variables required to create the plot.

Returning Variable Names for the Plotting Functions

The get_var_names subroutine returns the two variable names that are selected when the user clicks on one of the three plotting buttons. The function

Here is the code for get_var_names:

function [var1,var2] = get_var_names(handles)
list_entries = get(handles.listbox1,'String');
index_selected = get(handles.listbox1,'Value');
if length(index_selected) ~= 2
	errordlg('You must select two variables',...
			'Incorrect Selection','modal')
else
	var1 = list_entries{index_selected(1)};
	var2 = list_entries{index_selected(2)};
end 

Callbacks for the Plotting Buttons

The callbacks for the plotting buttons call get_var_names to get the names of the variables to plot and then call evalin to execute the plot commands in the base workspace.

For example, here is the callback for the plot function:

function plot_button_Callback(hObject, eventdata, handles)
[x,y] = get_var_names(handles);
evalin('base',['plot(' x ',' y ')'])

The command to evaluate is created by concatenating the strings and variables that result in the command:

plot(x,y)
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS