Creating Graphical User Interfaces |
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.
% --- Executes just before mygui is made visible. function mygui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to mygui (see VARARGIN) % Choose default command line output for mygui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes mygui wait for user response (see UIRESUME) % uiwait(handles.mygui);
handles.output = hObject
saves the handle of the figure, i.e., the handle of the GUI, for later access by the output function. For more information about the output function, see Output Function.
guidata(hObject,handles)
saves the handles
structure. You must use guidata
to save any changes that you make to the handles
structure. It is not sufficient just to set the value of a handles
field. See Managing GUI Data with the Handles Structure and The Handles Structure for more information.
uiwait(handles.mygui)
, initially commented out, blocks GUI execution until uiresume
is called or the GUI is deleted. Note that uiwait
allows the user access to other MATLAB windows to obtain information. Remove the comment symbol for this statement if you do want the GUI to be blocking when it opens. See Controlling Figure Window Behavior and Example: Using the Modal Dialog to Confirm an Operation for more information.
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.
Callbacks in Programmatically Created GUIs | Output Function |
© 1994-2005 The MathWorks, Inc.