Skip to Main Content Skip to Search
Product Documentation

Run a GUIDE GUI

Execute GUI Code

In most cases, you run your GUI by executing the code file that GUIDE generates by typing its name in the Command Window. This file loads the GUI figure and provides a framework for the component callbacks. For more information, see Files Generated by GUIDE.

Executing the code file raises a fully functional GUI. You can run a GUI in three ways, as described in the following sections. The last section illustrates how to test a GUI automatically by invoking its callbacks from a MATLAB script.

From the GUIDE Layout Editor

Run your GUI from the GUIDE Layout Editor by:

In either case, if the GUI has changed or has never been saved, GUIDE saves the GUI files before activating it and opens the GUI code file in your default editor. See Save a GUIDE GUI for information about this process. See Files Generated by GUIDE for more information about GUI code files.

From the Command Line

Run your GUI by executing its code file. For example, if your GUI code file is mygui.m, enter:

mygui

at the command line. The files must reside on your path or in your current folder. If you want the GUI to be invisible when it opens, enter:

mygui('Visible','off')

If a GUI accepts arguments when it is run, they are passed to the GUI's opening function. See Opening Function for more information.

From Any MATLAB Code File

Run your GUI from a script or function file by executing the GUI code file. For example, if your GUI code file is mygui.m, include the following statement in the script or function that invokes it.

mygui

If you want the GUI to be invisible when it opens, use this statement:

mygui('Visible','off')

The GUI files must reside on the MATLAB path or in the current MATLAB folder where the GUI is run.

If a GUI accepts arguments when it is run, they are passed to the GUI's opening function. See Opening Function for more information.

From a Test Script

You can test your GUI by executing its callbacks from a MATLAB script or function. Executing callbacks as you open a GUI also allows you to initialize it in a nondefault way. To enable this mode of opening, GUIDE provides a callback syntax for GUIs, documented in every code file GUIDE generates:

% gui_name('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in gui_name.m with the given input arguments.

Here is an example of a script that tests the GUIDE example GUI named simple_gui. The script loops three times to set the pop-up that specifies data to plot, and successively "clicks" each of three push buttons to generate a plot. Run this script from the Help browser by selecting the following code and pressing F9:

% Put GUI examples folder on path
addpath(fullfile(docroot,'techdoc','creating_guis','examples'))
% Launch the GUI, which is a singleton
simple_gui
% Find handle to hidden figure
temp = get(0,'showHiddenHandles');
set(0,'showHiddenHandles','on');
hfig = gcf;
% Get the handles structure
handles = guidata(hfig);
% Cycle through the popup values to specify data to plot 
for val = 1:3
    set(handles.plot_popup,'Value',val)
    simple_gui('plot_popup_Callback',...
               handles.plot_popup,[],handles)
    % Refresh handles after changing data
    handles = guidata(hfig);
    % Call contour push button callback
    simple_gui('contour_pushbutton_Callback',...
               handles.contour_pushbutton,[],handles)
    pause(1)
    % Call surf push button callback
    simple_gui('surf_pushbutton_Callback',...
               handles.contour_pushbutton,[],handles)
    pause(1)
    % Call mesh push button callback
    simple_gui('mesh_pushbutton_Callback',...
               handles.contour_pushbutton,[],handles)
    pause(1)
end
set(0,'showHiddenHandles',temp);
rmpath(fullfile(docroot,'techdoc','creating_guis','examples'))
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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