GUIs GUIDE Laying out the GUI Use GUIDE OpeningFcn GUIDE is going to create a function called NameofGUIOpeningFcn This function takes 4 variables in this order Handle to figure ID: 232154
Download Presentation The PPT/PDF document "Matlab" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Matlab GUIs
GUIDESlide2
Laying out the GUI
Use GUIDESlide3
OpeningFcn
GUIDE is going to create a function called ‘
NameofGUI_OpeningFcn
’
This function takes 4 variables, in this order
Handle to figure (
hObject
)
e
ventdata
, which is not used
A structure called
handles
v
arargin
, command line arguments to the GUISlide4
OpenFcn
is the initialization function, and is where you would place basic information & global variables
For example, for
simple_gui
all of the original data set up falls under
OpenFcn
%
Create the data to plot.
handles.peaks
=peaks(35);
handles.membrane
=membrane;
[
x,y
] = meshgrid(-8:.5:8);
r
= sqrt(x.^2+y.^2) +
eps
;
sinc
=
sin(r)./r
;
handles.sinc
=
sinc
;
% Set the current data value.
handles.current_data
=
handles.peaks
;
surf(handles.current_data
)Slide5
Handles
guidata(object_handle,data
)
stores the variable data as GUI data
Object_handle
is usually the parent figure
Data can be any MATLAB variable but is usually a structure called
handle
when using GUIDE
If you modify the
handle
you have to resave it using
guidata
Following on previous slide
%
Set the current data value.
handles.current_data
=
handles.peaks
;
surf(handles.current_data
)
% Update handles structure
guidata(hObject
, handles);Slide6
get and set
get – used to query handle graphics object properties
Useful in GUIs to figure out if radio buttons are on or off for example
s
et – used to set handle graphics object properties
Frequently used set the ‘String’ portion of the variables in the
handle
structure Slide7
GUIDE
Use the GUIDE interface to move components around
To design the components, you open Property Inspector from the ‘View’ drop-down menu
‘String’ is used to change the text associated with the component
‘Tag’ is used to control the name of the Callback function associated with the componen
tSlide8
Available Components
(look at guide)Slide9
Kinds of Callbacks (incomplete list)
ButtonDownFcn
–
executes when the GUI user presses a mouse button while the pointer is on or near a component or figure
Callback – Control action. Executes when a user clicks a push button or selects a menu item
ClickedCallback
– Control action. Executes when the push tool or toggle tool is clicked.Slide10
CreateFcn
– Initializes the component when a function creates it.
DeleteFcn
– Performs cleanup operations just before a component or figure is destroyed.
KeyPressFcn
– Executes when the user presses a keyboard key and the
callbacks’s
component or figure as focusSlide11
Panels and Button Groups
u
ipanel
: Panels arrange GUI components into groups, making the interface easier to understand.
ph =
uipanel(fh,'PropertyName',PropertyValue
,...
)
u
ibuttongroup
:
Button groups are like panels but are used to manage exclusive selection behavior for radio buttons and toggle buttons
bgh
=
uibuttongroup
(fh,'PropertyName
',PropertyValue
,...) Slide12
Panels and button groups are containers that arrange GUI components into groups.
If
you move the panel or button group, its children move with it and maintain their positions relative to the panel or button group
.
The panel or button group become the handle on calls to
uicontrolSlide13
ph =
uipanel(
'Parent',fh
,'Title','My
Panel',...
'Position',[.25 .1 .5 .8]);
pbh1
=
uicontrol(
ph
,'Style','pushbutton','String','Button
1',...
'
Units','normalized
',...
'Position',[.1 .55 .8 .3]);
pbh2
=
uicontrol(
ph
,'Style','pushbutton','String','Button
2',...
'
Units','normalized
',...
'Position',[.1 .15 .8 .3]);Slide14
Menu Bars
u
imenu
: add a menu bar (
ie
. File, Edit, View, etc)
mh
=
uimenu(parent,'PropertyName',PropertyValue
,...
)
Example
:
mh
=
uimenu(fh,'Label','My
menu');
eh1 =
uimenu(mh,'Label','Item
1');
eh2 =
uimenu(mh,'Label','Item
2','Checked','on')
;
seh1 = uimenu(eh1,'Label','Choice 1','Accelerator','C',...
'
Enable','off
');
seh2 = uimenu(eh1,'Label','Choice 2','Accelerator','H')Slide15
Reading in SAC data
Example we will look at is
speedyseis
The sac reader in this case is a script (not a function) called
get_sac
, which sets variables
filename
n
pts
s
ampdelta
d
ate
h
our
m
inu
s
eco
dataSlide16
Speedyseis
is written in GUIDE
It has a pushbutton called ‘Get Sac file’, which has the following callback
% --- Executes on button press in
getSACfilebutton
.
function
getSACfilebutton_Callback(hObject
,
eventdata
, handles)
global
hObject
handles
%
get_sac
reads sac file and puts data into workspace of calling program/routine
%uses
SAC_file_prompt
for prompt, if it does not exist - uses default prompt
%
SAC_file_prompt
='Select a SAC file';
get_sacSlide17
Next we set some handles
handles.filename
=filename;
handles.samprate
=round(1/sampdelta);
samprate_str
=num2str(handles.samprate);
set(handles.sampratedisplay,'String',samprate_str
);
handles.delt
=1/handles.samprate;
handles.fulldata
=data;
handles.fulldatalen
=
length(handles.fulldata
);Slide18
handles.fulldatend
=
length(handles.fulldata
);
handles.datstart
=1;
handles.datend
=
handles.fulldatalen
;
p
lotitall
%function defined later in the script
guidata(hObject
, handles)
;
%standard last line in a function in which
handles%have
been set or revisedSlide19
Available Components
axes: Axes enable your GUI to display graphics such as graphs
and images
using
commands such as: plot, surf, line, bar, polar, pie, contour, and mesh.
ah
= axes('Parent',fh,'Position',[.15 .15 .7 .7])
;Slide20
uicontrol
Check boxes can generate an action when checked and indicate their state as checked or not checked.
cbh
=
uicontrol(fh,'Style','checkbox
',...
'
String','Display
file extension',...
'Value',1,'Position',[30 20 130 20]
)
Edit text components are fields that enable users to enter or modify text strings.
eth =
uicontrol(fh,'Style','edit
',...
'
String','Enter
your name here.',...
'Position',[30 50 130 20])
;Slide21
u
icontrol
List boxes display items and enable users to select one or more items
lbh
=
uicontrol(fh,'Style','listbox
',...
'
String',{'one','two','three','four
'},...
'Value',1,'Position',[30 80 130 20])
;
Pop-up menus (drop down menus) open to display a list of choices
pmh
=
uicontrol(fh,'Style','popupmenu
',...
'
String',{'one','two','three','four
'},...
'Value',1,'Position',[30 80 130 20]);Slide22
u
icontrol
Push buttons generate an action when clicked
pbh
=
uicontrol(fh,'Style','pushbutton','String','Button
1',...
'Position',[50 20 60 40])
;
Radio buttons are similar to check boxes, but are typically mutually exclusive within a group of related radio buttons. Use button groups to manage related radio buttons
rbh
=
uicontrol(fh,'Style','radiobutton
',...
'
String','Indent
nested functions.',...
'Value',1,'Position',[30 20 150 20]);Slide23
u
icontrol
Sliders accept numeric input within a specified range by enabling the user to move a sliding bar
sh
=
uicontrol(fh,'Style','slider
',...
'Max',100,'Min',0,'Value',25,...
'SliderStep',[0.05 0.2],...
'Position',[30 20 150 30])
;
Static text controls display lines of text; typically used to label other controls
sth
=
uicontrol(fh,'Style','text
',...
'
String','Select
a data set.',...
'Position',[30 50 130 20]);Slide24
u
icontrol
Toggle buttons generate an action and indicate whether they are turned on or off.
tbh
=
uicontrol(fh,'Style','togglebutton
',...
'
String','Left
/Right Tile',...
'Value',0,'Position',[30 20 100 30
]);Slide25
u
itable
: Callbacks are fired when table cells are selected or edited. Tables can be made to be user editable.
th
= uitable(fh,'Data',magic(5));
tpos
=
get(th,'
Position
’)
texn
=
get(th,'
Extent
’)
tpos
(3) = texn(3);
tpos(4) = texn(4);
set(th
, '
Position’,
tpos
)