MATLAB Creating Graphical User Interfaces

(Barry) #1

8 Programming a GUIDE UI


command line to the opening function. The opening function can take actions with
them (for example, setting property values) and also make the arguments available to
callbacks by adding them to the handles structure.

For more information about using varargin, see the varargin reference page and
“Support Variable Number of Inputs”.
Passing Object Properties to an Opening Function

You can pass property name-value pairs as two successive command line arguments
when you run your program. If you pass a name-value pair that corresponds to a figure
property, MATLAB sets the property automatically. For example, my_gui('Color',
'Blue') sets the background color of the UI window to blue.

If you want your program to accept an input argument that is not a valid figure property,
then your code must recognize and handle that argument. Otherwise, the argument
is ignored. The following example is from the opening function for the Modal Question
Dialog template, available from the GUIDE Quick Start dialog box. The added code opens
the modal dialog with a message, specified from the command line or by another program
that calls this one. For example, this command displays the text, 'Do you want to
exit?' on the window.

myui('String','Do you want to exit?')

To accept this name-value pair, you must customize the opening function because
'String' is not a valid figure property. The Modal Question Dialog template file
contains code to performs these tasks:


  • Uses the nargin function to determine the number of user-specified arguments
    (which do not include hObject, eventdata, and handles)

  • Parses varargin to obtain property name/value pairs, converting each name string to
    lower case

  • Handles the case where the argument 'title' is used as an alias for the figure Name
    property

  • Handles the case 'string' , assigning the following value as a String property to
    the appropriate static text object
    function modalgui_OpeningFcn(hObject, eventdata, handles, varargin)
    .
    .
    .
    % Insert custom Title and Text if specified by the user
    % Hint: when choosing keywords, be sure they are not easily confused

Free download pdf