Documentation

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 App

This example shows how to examine and run a prebuilt GUIDE app. The app contains a list box that displays the variables in your MATLAB® workspace. The button below the list box refreshes the list. The three buttons on the right plot the selected variables using different scales for the x and y axes.

Open and Run the Example

To open the example in GUIDE, click the Open Script button on this page. To run the app, click the green Run Figure button at the top of the GUIDE window.

When you run the app, the list box displays your current workspace variables. Select one variable, and then hold the Ctrl key to select a second variable. Then click Plot, Semilogx, or Semilogy to plot the variables.

Examine the Code

  1. In GUIDE, click the Editor button to view the code.

  2. Near the top of the Editor window, use the Go To button to navigate to the functions discussed below.

update_button_Callback

The update_button_Callback function executes when the user clicks the Update Listbox button. It contains one command that calls another local function, update_listbox. (That function is kept separate so it can be reused elsewhere in the app.)

The update_listbox function executes the who command in the MATLAB workspace to get the list of current variables. Then it sets the contents of the list box to that list of variables.

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

plot_button_Callback

The plot_button_Callback function executes when the user presses the Plot button. The callbacks for the Semilogx and Semilogy buttons contain most of the same code.

First, the function calls the local function get_var_names, which returns the two selected variables in the list.

[x,y] = get_var_names(handles);

Then it checks to make sure at least one variable is selected. If no variables are selected, the callback returns and does not plot anything.

if isempty(x) && isempty(y)
    return
end

Finally, the plot command executes from within the base workspace.

try
    evalin('base',['plot(',x,',',y,')'])
catch ex
    errordlg(ex.getReport('basic'),...
            'Error generating linear plot','modal')
end
The catch block presents an error dialog box if an error occurs.

Related Examples

Was this topic helpful?