| MATLAB® | ![]() |
Many kinds of tasks can be thought of as initialization tasks. This is a sampling of some of them:
Define variables for supporting input and output arguments. See Declaring Variables for Input and Output Arguments.
Define default values for input and output arguments.
Define custom property values used for constructing the components. See Defining Custom Property/Value Pairs.
Process command line input arguments.
Create variables and data to be used by functions that are nested below the initialization section of the M-file. See Nested Functions in the MATLAB Programming Fundamentals documentation.
Define variables for data to be shared between GUIs.
Return user output if it is requested.
Update or initialize components.
Make changes needed to refine the look and feel of the GUI.
Make changes needed for cross-platform compatibility. See Designing for Cross-Platform Compatibility.
Make the GUI invisible while the components are being created and initialized. See Making the Figure Invisible.
Make the GUI visible when you are 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 | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |