Creating Graphical User Interfaces | ![]() ![]() |
The application M-file automatically includes some useful techniques for managing the GUI. These technique include:
Tag
property, generation of subfunction prototype, and assignment of Callback
property string.Opening the FIG-File
The application M-file uses the openfig
command to load the GUI figure. The actual command is
fig = openfig(mfilename,'reuse');
It is important to note that this statement derives the name of the FIG-file from the application M-file (the mfilename
command returns the name of the currently executing M-file). If you are using the application M-file generated by GUIDE, you must keep the names of the FIG-file and M-file the same. The reuse
argument specifies that there can be only a single instance of the GUI displayed at any time (see below).
Single vs. Multiple Instance of the GUI
One of the decisions you must make when designing GUIs is whether you want to allow multiple instances of the GUI figure to exist at one time.
If you choose to allow only a single instance of the GUI, subsequent attempts to create another GUI figure simply bring the existing GUI to the front of other windows. Most informational dialogs (particularly if modal) should be created in singleton mode since it is not desirable to allow user actions to create more dialogs.
The GUIDE Layout Editor is an example of a GUI for which you can have multiple instances. This GUI was designed to enable users to have a number of layouts open simultaneously.
Positioning the GUI Onscreen
The application M-file uses the movegui
command to ensure the GUI figure is visible on the screen of the target computer, regardless of the screen size and resolution. If the specified figure position would result in the GUI being placed off screen, movegui
moves the figure to the nearest on-screen location with respect to the specified position.
The statement in the application M-file is
movegui(fig,'onscreen')
where fig
is the handle of GUI figure returned by the openfig
command.
movegui
also provides other options for GUI placement.
Creating and Storing the Handles Structure
When you launch the GUI, the application M-file creates a structure that contains the handles of all the components in the GUI. It then saves this structure in the figure's application data so that it can be retrieved when needed (e.g., from a callback routine subfunction).
The name of the structure field containing a given object's handle is taken from the object's Tag
property. For example, an object with a Tag
value of pushbutton1
is accessed with
handles.pushbutton1
You can access the figure's hidden handle in a similar way. If the figure Tag
is figure1
, then
handles.figure1
The application M-files uses guihandles
and guidata
to create and store the structure.
handles = guihandles(fig); % Create handle struct guidata(fig,handles); % Save struct in figure's app data
Note that only those components whose Tag
property is set to a string that is a valid variable name are included in this structure. Use isvarname
to determine if a string is a valid name.
The handles
structure is one of the arguments passed to each callback. You can also use this same structure to save data and pass it between subfunctions. See Managing GUI Data for a discussion of how to use the handles
structure for other data.
![]() | Execution Paths in the Application M-File | Managing GUI Data | ![]() |