Initializing the GUI

Many kinds of tasks can be thought of as initialization tasks. This is a sampling of some of them:

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.

Examples

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:

Declaring Variables for Input and Output Arguments

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
                       % returns

See the varargin reference page and the Icon Editor M-file for more information.

Defining Custom Property/Value Pairs

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 initialize

Each 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

See the complete Icon Editor M-file for more information.

Making the Figure Invisible

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.

Returning Output to the User

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,

  1. A call to uiwait blocks execution until uiresume is called or the current figure is deleted.

  2. While execution is blocked, the GUI user creates the desired icon.

  3. 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.

  4. 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{:};
    end

mIconData 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.

  


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