Академический Документы
Профессиональный Документы
Культура Документы
What Is a GUI?
A graphical user interface (GUI) is a graphical display in one or more windows
containing controls, called components, which enable a user to perform
interactive tasks. The user of the GUI does not have to create a script or type
commands at the command line to accomplish the tasks. Unlike coding programs
to accomplish tasks, the user of a GUI need not understand the details of how
the tasks are performed.
GUI components can include menus, toolbars, push buttons, radio buttons, list
boxes, and slidersjust to name a few. GUIs created using MATLAB tools can
also perform any type of computation, read and write data files, communicate
with other GUIs, and display data as tables or as plots.
The following figure illustrates a simple GUI that you can easily build yourself.
Page 1
Page 2
Page 3
This section shows you how to use GUIDE to create the graphical user interface
(GUI) shown in the following figure.
Start GUIDE by typing guide at the MATLAB prompt. The GUIDE Quick Start
dialog displays, as shown in the following figure.
Page 4
In the GUIDE Quick Start dialog box, select the Blank GUI (Default) template.
Click OK to display the blank GUI in the Layout Editor, as shown in the following
figure.
Display the names of the GUI components in the component palette. Select File
> Preferences. Then select GUIDE > Show names in component palette, and
then click OK. The Layout Editor then appears as shown in the following figure
Page 5
Add different components, your GUI should now look like this in the Layout Editor.
The push buttons, pop-up menu, and static text have default labels when you
create them. Their text is generic, for example Push Button 1. Change the text to
be specific to your GUI, so that it explains what the component is for.
Page 6
1.
2.
3.
Page 7
Replace the existing text with the names of the three data sets: Peaks,
Membrane, and Sinc. Press Enter to move to the next line.
4. When you have finished editing the items, click OK. The first item in your
list, Peaks, appears in the pop-up menu in the layout area.
Page 8
GUIDE saves the files simple_gui.fig and simple_gui.m and activates the GUI.
It also opens the GUI code file in your default editor.
Display the opening function in the MATLAB editor. If the file simple_gui.m
is not already open in your editor, open it by selecting Editor from the
GUIDE View menu. In the editor, click the function icon on the toolbar,
Page 9
The cursor moves to the opening function, which already contains this code:
2.
Create data for the GUI to plot by adding the following code to the
opening function immediately after the comment that begins % varargin...
Page 10
Page 11
Display the pop-up menu callback in the MATLAB Editor. Right-click the
pop-up menu component in the GUIDE Layout Editor to display a context
menu. From that menu, select View Callbacks > Callback.
2.
Page 12
Display the Surf push button callback in the MATLAB Editor. Right-click the
Surf push button in the Layout Editor to display a context menu. From that
menu, select View Callbacks > Callback.
Page 13
Add the following code to the callback immediately after the comment
that begins % handles...
%Display surf plot of the currently selected data.
surf(handles.current_data);
3.
Repeat steps 1 and 2 to add similar code to the Mesh and Contour push
button callbacks.
Add
this
code
pushbutton2_Callback:
to
theMesh
push
button
callback,
Addthiscodetothe
pushbutton3_Callback:
Contour
push
button
callback,
Save your code by selecting Save from the File menu. Your GUI is ready to
run.
Page 14
Page 15
Page 16
Altering the Tag causes GUIDE to set the field name for the frequency plot axes
to frequency_axes in the handles structure. Within the plot_button_Callback, you
access that axes handle with handles.frequency_axes. You use the handle as the
first argument to plot to ensure that the graph is displayed in the correct axes, as
follows:
plot(handles.frequency_axes,f,m(1:257))
Likewise, the Tag ofthetimeaxesissetto time_axes,andthecallto plot uses it as
follows:
plot(handles.time_axes,t,x)
Page 17
Plot Data
GUI Using Matlab
Page 18
Page 19
plot_button_Callback(hObject,
eventdata,
handles,
Page 20
Page 21
Page 22
Page 23
Page 24
Page 25
Page 26
Page 27
The tilt transform applies a rotation about the x-axis of 0.5091 radians (equal to
23.44 degrees, the inclination of the Earths axis of rotation). The rotate
transform initially has a default identity matrix. The spinstopbutton_Callback
subsequently updates the matrix to rotate about the z-axis by 0.01745329252
radians (1 degree) per iteration, using the following code:
GUI Using Matlab
Page 28
Page 29
Page 30
Page 31
Page 32
Perform the following steps in GUIDE and in the Property Inspector to generate
the layout, thereby creating the following objects:
1.
Using the Panel tool , drag out the three uipanels in the positions that are
shown above. Keep the defaults for their Tag properties (which are uipanel1,
uipanel2,and uipanel3). Create, in order:
A long panel on the left, renaming its Title to Data Set in the Property
Inspector.
A panel on the lower right, half the height of the first panel, renaming its
Title to Data Statistics in the Property Inspector.
A panel above the Data Statistics panel, renaming its Title to Sunspots v.
Year Plots in the Property Inspector. This panel changes its name when the type
of plot that is displayed changes.
2.
Using the Table tool , drag out a uitable inside the Data Set panel, setting
these properties in the Property Inspector to nondefault values:
Page 33
CellSelectionCallback,
which
GUIDE
automatically
sets
to
data_table_CellSelectionCallback and declares in the code file when you click the
pencil-and-paper icon.
3.
Drag out a second uitable, inside the Data Statistics panel, setting these
properties in the Property Inspector:
BackgroundColor to yellow (using the color picker).
ColumnName to Population and Selection.
Tag to data_stats.
TooltipString to statistics for table and selection.
RowName to nine strings: N, Min, Max, Mean, Median, Std Dev, 1st Year,
Last Year,and Est. Period.
You can conveniently set these labels with the Table Property Editor as follows:
a.
b.
Page 34
Use the Axes tool to drag out an axes within the top half of the Sunspots v.
Year Plots panel, leaving its name as axes1.
5. Drag out a second axes, leaving its name as axes2 inside the Sunspots v.
Year Plots panel, directly below the first axes. Leave enough space below each
axes to display the x-axis labels.
6. Identify the axes withlabels.Usingthe Text tool, drag out a small rectangle in
the upper right corner of the upper axes (axes1). Double-click it, and in the
Property Inspector, change its String property to Population and its Tag
property to poplabel.
7. Place a second label in the lower axes (axes2), renaming this text object
Selection and setting its Tag property to sellabel.
8. Create a title for the GUI. Using the Text tool, drag out a static text object at
the top left of the GUI, above the data table. Double-click it, and in the
Property Inspector, change its String property to Zurich Sunspot Statistics,
1700-1987 and its FontWeight property to bold.
9. Add a prompt above the axes; place a text label just above the Sunspots v.
Year Plots panel, near its right edge. Change its Tag property to newfig, its
String property to Right-click plots for larger view and its FontAngle property
to Italic.
10. Makeapop-upmenutospecifythetypeofgraphtoplot.Usingthe Pop-up Menu
tool , drag out a pop-up menu just above the Sunspots v. Year panel, aligning
it to the panels left edge. In the property Inspector, set these properties:
String to
Sunspots v. Year Plots
FFT Periodogram Plots
Tag to plot_type
Tooltip to Choose type of data plot
Click the Callback propertys icon. This creates a declaration called
plot_type_Callback, to which you add code later on.
11. Select the Push Button tool , and drag out a push button in the upper right of
the figure. In the Property Inspector, rename it to Quit and set up its callback
as follows:
Double-click it and in the Property Inspector, set its Tag property to quit
and its String property to Quit.
Click the Callback property to create a callback for the button in the
code file tablestat.m. GUIDE sets the Callback of the Quit item to
quit_Callback.
Inthecodefile,forthe quit_Callback function. enter:
close(ancestor(hObject,'figure'))
12.Save the GUI in GUIDE, naming it tablestat.fig. This action also saves the code
file as tablestat.m.
GUI Using Matlab
Page 35
2.
3.
4.
5.
6.
In the Command Window, access the sunspot demo data set. Enter: load
sunspot.dat. The variable sunspot, a 288-by-2 double array, is displayed
in the MATLAB workspace.
Double-click the Data Set table to open the Property Inspector for the
data table.
In the Property Inspector, click the Table Editor icon to the right of the
Data property to open the Table Property Editor.
In the Table Property Editor, select Table from the list in the left-hand
column.
Select the bottom radio button, Change data value to the selected
workspace variable below.
From the list of workspace variables in the box below the radio button,
select sunspot and click OK. GUIDE inserts the sunspot data in the table.
Page 36
The stats matrix is a 9-by-2 cell array in which each row is a separate statistic
computed from the table argument. The last statistic is not computed by
setStats; it comes from the plotPeriod function when it computes and plots the
FFT periodogram and is passed to setStats as the peak parameter.
Page 37
The function updates the Data Statistics table and the plots. To perform the
updates, it calls the refreshDisplays function twice, which is a custom function
added to the GUI code file. In between the two calls, the refreshDisplays function
retrieves row indices for the current selection from the currSelection member of
the
handles
structure,
where
they
were
cached
by
the
data_table_CellSelectionCallback.
You can see the effect of toggling the plot type in the two illustrations that follow.
The one on the left shows the Sunspots v. Year plots, and the one on the right
shows the FFT Periodograms Plots. The selection in both cases is the years 1901
1950.
Page 38
Page 39
The
user
selects
data_table_CellSelectionCallback.
cells
in
the
data
table,
its
Page 40
Page 41
3.
4.
5.
6.
Click the Context Menus tab to select the type of menu you are creating.
Click the New Context Menu icon . This creates a context menu in the
Menu Editor workspace called untitled. It has no menu items and is not
attached to any GUI object yet.
Select the new menu and in the Tag edit field in the Menu Properties
panel, type plot_axes1.
Click the New Menu Item icon . A menu item is displayed underneath the
plot_axes1 item in the Menu Editor workspace.
In the Menu Properties panel, type Open plot in new window for Label and
plot_ax1 for Tag. Do not set anything else for this item.
Repeat the last four steps to create a second context menu:
Make the Tag for the menu plot_axes2.
Create a menu item under it and make its Label Open plot in new
window and assign it a Tag of plot_ax2.
7.
Page 42
In the GUIDE Layout Editor, double-click axes1 (the top axes in the upper
right corner) to open it in the Property Inspector.
2. Click the right-hand column next to UIContextMenu to see a drop-down
list.
3. From the list, select plot_axes1.
Perform the same steps for axes2,butselect plot_axes2 as its UIContextMenu.
Page 43
Page 44
Upon Clicking Open plot in new window, a new figure is displayed with
the following content.
Page 45
Page 46