Matlab GUIs

advertisement
Matlab GUIs
GUIDE
Laying out the GUI
Use GUIDE
OpeningFcn
 GUIDE is going to create a function called
‘NameofGUI_OpeningFcn’
 This function takes 4 variables, in this order




Handle to figure (hObject)
eventdata, which is not used
A structure called handles
varargin, command line arguments to the GUI
 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)
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);
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
 set – used to set handle graphics object properties
 Frequently used set the ‘String’ portion of the variables in the
handle structure
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 component
Available Components
 (look at guide)
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.
 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 focus
Panels and Button Groups
 uipanel: Panels arrange GUI components into groups,
making the interface easier to understand.
ph = uipanel(fh,'PropertyName',PropertyValue,...)
 uibuttongroup: Button groups are like panels but are
used to manage exclusive selection behavior for radio
buttons and toggle buttons
bgh = uibuttongroup(fh,'PropertyName',PropertyValue,...)
 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 uicontrol
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]);
Menu Bars
 uimenu: 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')
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
npts
sampdelta
date
hour
minu
seco
data
 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_sac
 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);
handles.fulldatend=length(handles.fulldata);
handles.datstart=1;
handles.datend=handles.fulldatalen;
plotitall %function defined later in the script
guidata(hObject, handles);
%standard last line in a function in which
handles%have been set or revised
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]);
 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]);
 uicontrol
 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]);
 uicontrol
 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]);
 uicontrol
 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]);
 uicontrol
 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]);
 uitable: 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)
Download