Initialize a Programmatic GUI

When you open a GUI, it usually initializes certain data structures and variable values. These actions can include:

  • Defining default values

  • Setting UI component property values

  • Processing input arguments

  • Hiding the figure window until all the components are created

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.

Examples

Declare Variables for Input and Output Arguments

These are typical declarations for input and output arguments.

mInputArgs = varargin; % Command line arguments when invoking
                       % the GUI
mOutputArgs = {};      % Variable for storing output when GUI
                       % returns

See the varargin reference page for more information.

Define Custom Property/Value Pairs

This 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 start the Icon Editor application. The first statement creates a new icon. The second statement opens existing icon file for editing.

cdata = iconEditor('iconwidth',16,'iconheight',25)
cdata = iconEditor('iconfile','eraser.gif');

iconEditor calls a routine, processUserIputs, during the initialization to accomplish these tasks:

  • 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

Make the Figure Invisible

When you create the GUI figure, make it invisible when you create it. Display it only after you have added all the UI components.

To make the GUI invisible, set the figure Visible property to 'off' when you create the figure:

hMainFigure = figure(...
              'Units','characters',...
              'MenuBar','none',...
              'Toolbar','none',...
              'Position',[71.8 34.7 106 36.15],...
              'Visible','off');

After you have added all the components to the figure window, make the figure visible:

hMainFigure.Visible = 'on';

Most components have a Visible property. Thus, you can also use this property to make individual components invisible.

Return 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 main function returns.

In the example shown here,

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

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

  3. When the user clicks OK, that push button's callback calls the uiresume function.

  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 EditorIcon Editor code file for more information.

Was this topic helpful?