| MATLAB® | ![]() |
| On this page… |
|---|
Workspace Variable Example Outcome Techniques Used in This Example |
This GUI uses a list box to display workspace variables, which the user can then plot.
Populate the list box with the variable names that exist in the base workspace.
Display the list box with no items initially selected.
Enable multiple item selection in the list box.
Update the list items when the user press a button.
Evaluate the plotting commands in the base workspace.
The following figure illustrates the layout.

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:
Leave the empty list box callback in the GUI M-file.
Delete the string assigned to the list box Callback property.
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.
Note The following links execute MATLAB commands and are designed to work within the MATLAB Help browser. If you are reading this online or in PDF, you should go to the corresponding section in the MATLAB Help Browser to use the links. |
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.
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.
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.
List box multiple selection follows the standard for most systems:
Ctrl+click left mouse button — noncontiguous multi-item selection
Shift+click left mouse button — contiguous multi-item selection
Users must use one of these techniques to select the two variables required to create the plot.
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
Gets the list of all items in the list box from the String property.
Gets the indices of the selected items from the Value property.
Returns two string variables, if there are two items selected. Otherwise get_var_names displays an error dialog explaining that the user must select two variables.
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 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)
![]() | List Box Directory Reader | A GUI to Set Simulink® Model Parameters | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |