Creating Graphical User Interfaces Previous page   Next Page

Initialization Callbacks in GUIDE

GUIDE automatically includes two callbacks, the opening function and the output function, in every GUI M-file it creates.

For information about other callbacks, see Component and Figure Callbacks.

Opening Function

The opening function is the first callback in every GUI M-file. It is executed just before the GUI is made visible to the user, but after all the components have been created, i.e., after the components' CreateFcn callbacks, if any, have been run.

You can use the opening function to perform your initialization tasks before the user has access to the GUI. For example, you can use it to create data or to read data from an external source.

Naming and Template

GUIDE names the opening function by appending _OpeningFcn to the name of the M-file. This is an example of an opening function template as it might appear in the mygui M-file.

In this template:

Input Arguments

The opening function has four input arguments hObject, eventdata, handles, and varargin. The first three are the same as described in Component and Figure Callbacks. varargin enables you to pass arguments to the opening function from the command line. For more information about varargin, see the varargin reference page and Passing Variable Numbers of Arguments in the MATLAB documentation.

All command line arguments are passed to the opening function via varargin. If you open the GUI with a property name/property value pair as arguments, the GUI opens with the property set to the specified value. For example, my_gui('Position', [71.8 44.9 74.8 19.7]) opens the GUI at the specified position, since Position is a valid figure property.

If the input argument is not a valid figure property, you must add code to the opening function to make use of the argument. For an example, look at the opening function for the modal question dialog template. The added code enables you to open the modal dialog with the syntax

which displays the text 'Do you want to exit' on the GUI. In this case, it is necessary to add code to the opening function because 'String' is not a valid figure property.


Previous page  Callbacks in Programmatically Created GUIs Output Function Next page

© 1994-2005 The MathWorks, Inc.