| Contents | Index |
| On this page… |
|---|
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.
Note You can display the GUI figure by double-clicking its file name in the Current Folder Browser. You can also display it by executing openfig, open, or hgload. These functions load a FIG-file into the MATLAB workspace and open the figure for viewing. If the displayed figure is a GUIDE GUI, you can manipulate its components, but nothing happens because no corresponding code file is running to initialize the GUI or execute component callbacks. |
Run your GUI from the GUIDE Layout Editor by:
Clicking the
button on the Layout Editor toolbar
Selecting Run from the Tools menu
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.
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.
Note Consider whether you want to allow more than one copy of the GUI to be active at the same time. If you want only one GUI to be active, select Options > GUI Allows Only One Instance to Run (Singleton) from the Layout Editor View menu. See GUI Options for more information. |
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.
Note Consider whether you want to allow more than one copy of the GUI to be active at the same time. If you want only one GUI to be active, select Options from the Layout Editor View menu, then select GUI Allows Only One Instance to Run (Singleton). See GUI Options for more information. |
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'))
![]() | Save a GUIDE GUI | Programming a GUIDE GUI | ![]() |

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 |