20-12-2012, 05:55 PM
MATLAB The Language of Technical Computing
MATLAB The Language.pdf (Size: 1.22 MB / Downloads: 29)
Introduction
A graphical user interface (GUI) is a user interface built with graphical objects,
such as buttons, text fields, sliders, and menus. In general, these objects
already have meanings to most computer users. For example, when you move
a slider, a value changes; when you press an “OK” button, your settings are
applied and a dialog box is dismissed. Of course, to leverage this built-in
familiarity, you must be consistent in how you use the various GUI-building
components.
Applications that provide GUIs are generally easier to learn and use since the
person using the application does not need to know what commands are
available or how they work. The action that results from a particular user
action can be made clear by the design of the interface.
The sections that follow describe how to create GUIs with MATLAB. This
includes laying out the components, programming them to do specific things in
response to user actions, and saving and launching the GUI; in other words,
the mechanics of creating GUIs.
Creating GUIs with GUIDE
MATLAB implements GUIs as figure windows containing various styles of
uicontrol objects. You must program each object to performthe intended action
when activated by the user of the GUI. In addition, you must be able to save
and launch your GUI. All of these tasks are simplified by GUIDE, MATLAB’s
Graphical User Interface Development Environment.
GUI Development Environment
The process of implementing a GUI involves two basic tasks:
•Laying out the GUI components
•Programming the GUI components
GUIDE is primarily a set of layout tools. However, GUIDE also generates an
M-file that contains code to handle the initialization and launching of the GUI.
This M-file also provides a framework for the implementation of the callbacks
– the functions that execute when users activate a component in the GUI.
The Implementation of a GUI
While it is possible to write anM-file that contains all the commands to lay out
a GUI, it is easier to use GUIDE to lay out the components interactively and to
generate two files that save and launch the GUI:
•A FIG-file – contains a complete description of the GUI figure and all of its
children (uicontrols and axes), as well as the values of all object properties.
•An M-file – contains the functions that launch and control the GUI and the
callbacks, which are defined as subfunctions. ThisM-file is referred to as the
application M-file in this documentation.
Note that the application M-file does not contain the code that lays out the
uicontrols; this information is saved in the FIG-file.
Features of the GUIDE-Generated Application M-File
GUIDE simplifies the creation of GUI applications by automatically
generating an M-file framework directly from your layout. You can then use
this framework to code your application M-file. This approach provides a
number of advantages:
•The M-file contains code to implement a number of useful features (see
Configuring Application Options for information on these features).
•The M-file adopts an effective approach to managing object handles and
executing callback routines (see Creating and Storing the Object Handle
Structure for more information).
•The M-files provides a way to manage global data. (see Managing GUI Data
for more information).
•The automatically inserted subfunction prototypes for callback routines
ensure compatibility with future releases. For more information, see
Generating Callback Function Prototypes for information on syntax and
arguments.
Beginning the Implementation Process
To begin implementing your GUI, proceed to the following sections:
•Selecting GUIDE Application Options – to set both FIG-file and M-file
options.
•Using the Layout Editor – to begin laying out the GUI.
•Understanding the Application M-File – to understand programming
techniques used in the application M-file.
•Application Techniques – to see a collection of examples that illustrate
techniques that are useful for implementing GUIs.
Selecting GUIDE Application Options
Issuing the guide command displays an empty Layout Editor with an untitled
figure. Before adding components to the layout, you should configure the GUI
using the GUIDE Application Options dialog. Access the dialog by selecting
Application Options from the Layout Editor Tools menu.
Configuring the Application M-file
The GUIDE Application Options dialog enables you to select whether you want
GUIDE to generate only a FIG-file for your layout or both a FIG-file and its
companion application M-file. You can also select a number of different
behaviors for your GUI.
Allowing Proportional GUI Resizing
Use this approach if you want to allow users to resize the GUI and are satisfied
with a behavior that simply scales each component’s size and relative position
within the figure window.Note that the font size of component labels does not
resize and, if the size is reduced enough, these labels may become unreadable.
User-Specified Resize Operation
You can create GUIs that accommodate resizing, while at the same time
maintain the appearance and usability of your original design by programming
the figure ResizeFcn callback routine. This callback routine essentially
recalculates the size and position of each component based on the new figure
size.
This approach to handling figure resizing is used most typically in GUI-based
applications that require user interaction on an ongoing basis. Such an
application might contain axes for displaying data and various components
whose position and size are critical to the successful use of the interface.
Command-Line Accessibility
When MATLAB creates a graph, the figure and axes are included in the list of
children of their respective parents and their handles are available through
commands such as findobj, set, and get. If you issue another plotting
command, the output is directed to the current figure and axes.
GUIs are also created in figure windows. Generally, you do not want GUI
figures to be available as targets for graphics output, since issuing a plotting
command could direct the output to the GUI figure, resulting in the graph
appearing in the middle of the GUI.
In contrast, if you create a GUI that contains an axes, such as a plotting tool,
users need access to the figure. In this case, you should enable command-line
access.
Electing to Generate Only the FIG-File
Select Generate .fig file only in the GUIDE Application Options dialog if you
do not want GUIDE to generate an application M-file. When you save the GUI
from the Layout Editor, GUIDE creates a FIG-file, which you can redisplay
using the open or hgload command.
When you select this option, you must set the Callback property of each
component in your GUI to a string thatMATLAB can evaluate and perform the
desired action. This string can be an expression or the name of an M-file.
Select this option if you want to follow a completely different programming
paradigm than that generated by the application M-file.