| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
When you open a GUI, it usually initializes certain data structures and variable values. These actions can include:
Defining variables for supporting input and output arguments. See Declaring Variables for Input and Output Arguments.
Defining default values for input and output arguments.
Defining custom property values used for constructing the components. See Defining Custom Property/Value Pairs.
Processing command line input arguments.
Creating variables used by functions that are nested below the initialization section of the M-file. See Nested Functions in the MATLAB Programming Fundamentals documentation.
Defining variables for sharing data between GUIs.
Returning user output when requested.
Updating or initializing components.
Changing or refining the look and feel of the GUI.
Adapting the GUI to work across platforms. See Designing for Cross-Platform Compatibility.
Hiding the GUI until all its components are ready to use. See Making the Figure Invisible.
Showing the GUI when it is ready for the user to see it.
Group these tasks together rather than scattering them throughout the code. If an initialization task is long or complex, consider creating a utility function to do the work.
Typically, some initialization tasks appear in the M-file before the components are constructed. Others appear after the components are constructed. Initialization tasks that require the components must appear following their construction.
These are some initialization examples taken from the examples discussed in Examples of GUIs Created Programmatically. If MATLAB software is running on your system, you can use these links to see the complete M-files:
These are typical declarations for input and output arguments. They are taken from example Icon Editor.
mInputArgs = varargin; % Command line arguments when invoking
% the GUI
mOutputArgs = {}; % Variable for storing output when GUI
% returnsSee the varargin reference page and the Icon Editor M-file for more information.
The example Icon Editor defines property value pairs to be used as input arguments.
The example defines the properties in a cell array, mPropertyDefs, and then initializes the properties.
mPropertyDefs = {...
'iconwidth', @localValidateInput, 'mIconWidth';
'iconheight', @localValidateInput, 'mIconHeight';
'iconfile', @localValidateInput, 'mIconFile'};
mIconWidth = 16; % Use input property 'iconwidth' to initialize
mIconHeight = 16; % Use input property 'iconheight' to initialize
mIconFile = fullfile(matlabroot,'toolbox/matlab/icons/');
% Use input property 'iconfile' to initializeEach row of the cell array defines one property. It specifies, in order, the name of the property, the routine that is called to validate the input, and the name of the variable that holds the property value.
The fullfile function builds a full filename from parts.
The following statements each start the Icon Editor. The first one could be used to create a new icon. The second one could be used to edit an existing icon file.
cdata = iconEditor('iconwidth',16,'iconheight',25)
cdata = iconEditor('iconfile','eraser.gif');iconEditor calls a routine, processUserIputs, during the initialization to
Identify each property by matching it to the first column of the cell array
Call the routine named in the second column to validate the input
Assign the value to the variable named in the third column
See the complete Icon Editor M-file for more information.
When you create the GUI figure, make it invisible so that you can display it for the user only when it is complete. Making it invisible during creation also enhances performance.
To make the GUI invisible, set the figure Visible property to off. This makes the entire figure window invisible. The statement that creates the figure might look like this:
hMainFigure = figure(...
'Units','characters',...
'MenuBar','none',...
'Toolbar','none',...
'Position',[71.8 34.7 106 36.15],...
'Visible','off');Just before returning to the caller, you can make the figure visible with a statement like the following:
set(hMainFigure,'Visible','on')
Most components have Visible properties. You can also use these properties to make individual components invisible.
If your GUI function provides for an argument to the left of the equal sign, and the user specifies such an argument, then you want to return the expected output. The code that provides this output usually appears just before the GUI returns.
In the example shown here, taken from the Icon Editor example M-file,
A call to uiwait blocks execution until uiresume is called or the current figure is deleted.
While execution is blocked, the GUI user creates the desired icon.
When the user signals completion of the icon by clicking OK, the routine that services the OK push button calls uiresume and control returns to the statement following the call to uiwait.
The GUI then returns the completed icon to the user as output of the GUI.
% Make the GUI blocking.
uiwait(hMainFigure);
% Return the edited icon CData if it is requested.
mOutputArgs{1} = mIconCData;
if nargout>0
[varargout{1:nargout}] = mOutputArgs{:};
endmIconData contains the icon that the user created or edited. mOutputArgs is a cell array defined to hold the output arguments. nargout indicates how many output arguments the user has supplied. varargout contains the optional output arguments returned by the GUI. See the complete Icon Editor M-file for more information.
![]() | Introduction | Callbacks: An Overview | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |