Plot Workspace Variables in a GUIDE App

Note

The GUIDE environment will be removed in a future release. After GUIDE is removed, existing GUIDE apps will continue to run in MATLAB® but they will not be editable in GUIDE.

To continue editing an existing GUIDE app, see GUIDE Migration Strategies for information on how to help maintain compatibility of the app with future MATLAB releases. To create new apps, use App Designer instead.

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 App

Open and run the app. Select one variable in the list box, 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 Topics