This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Initialize a Programmatic App

Some apps might perform these tasks when you launch them:

  • Define default values

  • Set UI component property values

  • Process input arguments

  • Hide the figure window until all the components are created

When you develop an app, consider grouping these tasks together in your code file. If an initialization task involves several steps, consider creating a separate function for that task.


Declare Variables for Input and Output Arguments

These are typical declarations for input and output arguments.

mInputArgs = varargin; % Command line arguments
mOutputArgs = {};      % Variable for storing output

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 figure window, make it invisible when you create it. Display it only after you have added all the UI components.

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

hMainFigure = figure(...
              'Position',[71.8 34.7 106 36.15],...

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 program allows an output argument, 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 program's 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 program returns the completed icon to the user as output.

    % Make the window blocking.
    % Return the edited icon CData if it is requested.
    mOutputArgs{1} = mIconCData;
    if nargout>0
        [varargout{1:nargout}] = mOutputArgs{:};

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 program. See the complete Icon Editor code file for more information.

Related Examples

Was this topic helpful?