This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Plot Workspace Variables in a GUIDE UI

About the Example

This example shows how to create a UI that uses a list box to display names of variables in the base workspace, and then plot them. Initially, no variable names are selected in the list box. The UI contains controls that perform these tasks:

  • Update the list.

  • Select multiple variables in the list box. Exactly two variables must be selected.

  • Create linear, semilogx and semilogy line graphs of selected variables.

The program evaluates the plotting commands in the base workspace. It does no validation before plotting. When you use this UI, you are responsible for selecting pairs of variables that can be plotted against one another. The top-most selection is used as the x-variable, the lower one as the y-variable.

To get and view the example code:

  1. Copy the example FIG-file and code file to your current (writeable) folder and open the FIG-file in GUIDE with the following commands:

    copyfile(fullfile(docroot, 'techdoc','creating_guis',...
    'examples','lb.*')), fileattrib('lb.*', '+w')
    guide lb.fig

  2. From GUIDE Layout Editor, click the Editor button .

    The lb.m code displays in the MATLAB® Editor.

Read Workspace Variables

When the UI initializes, it queries the workspace variables and sets the list box String property to display these variable names. Adding the following local function to the UI code, lb.m, 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');

The function input argument is the handles structure set up by the GUIDE. This structure contains the handle of the list box, as well as the handles of all other components in the UI.

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

Read Selections from List Box

To use the UI, select two variables from the workspace and then choose one of three plot commands to create a graph: plot, semilogx, or semilogy.

No callback for the list box exists in the code file. One is not needed because the plotting actions are initiated by push buttons.

Enable Multiple Selection

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

Selecting Multiple Items

List box 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

Use one of these techniques to select the two variables required to create the plot.

Return Variable Names for the Plotting Functions

The local function, get_var_names, returns the two variable names that are selected when you click one of the three plotting buttons. The function does these tasks:

  • Gets all the 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 box stating that you 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')
	var1 = list_entries{index_selected(1)};
	var2 = list_entries{index_selected(2)};

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, and looks like this:

catch ex
      ex.getReport('basic'),'Error generating semilogx plot','modal')

The try/catch block handles errors resulting from attempting to graph inappropriate data. When evaluated, the result of the command is:


The other two plotting buttons work in the same way, resulting in semilogx(x,y) and semilogy(x,y).

Related Examples

More About

Was this topic helpful?