Matlab GUIs
description
Transcript of Matlab GUIs
![Page 1: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/1.jpg)
Matlab GUIsGUIDE
![Page 2: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/2.jpg)
Laying out the GUI
Use GUIDE
![Page 3: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/3.jpg)
OpeningFcnGUIDE is going to create a function called
‘NameofGUI_OpeningFcn’
This function takes 4 variables, in this orderHandle to figure (hObject)eventdata, which is not usedA structure called handlesvarargin, command line arguments to the GUI
![Page 4: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/4.jpg)
OpenFcn is the initialization function, and is where you would place basic information & global variablesFor 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)
![Page 5: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/5.jpg)
Handlesguidata(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 structureguidata(hObject, handles);
![Page 6: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/6.jpg)
get and setget – used to query handle graphics object
propertiesUseful 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
![Page 7: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/7.jpg)
GUIDEUse 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
![Page 8: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/8.jpg)
Available Components(look at guide)
![Page 9: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/9.jpg)
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.
![Page 10: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/10.jpg)
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
![Page 11: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/11.jpg)
Panels and Button Groupsuipanel: 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,...)
![Page 12: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/12.jpg)
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
![Page 13: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/13.jpg)
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]);
![Page 14: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/14.jpg)
Menu Barsuimenu: 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')
![Page 15: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/15.jpg)
Reading in SAC dataExample we will look at is speedyseis
The sac reader in this case is a script (not a function) called get_sac, which sets variables
filenamenptssampdeltadatehourminusecodata
![Page 16: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/16.jpg)
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
![Page 17: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/17.jpg)
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);
![Page 18: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/18.jpg)
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
![Page 19: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/19.jpg)
Available Componentsaxes: 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]);
![Page 20: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/20.jpg)
uicontrolCheck 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]);
![Page 21: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/21.jpg)
uicontrolList boxes display items and enable users to select
one or more itemslbh = 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]);
![Page 22: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/22.jpg)
uicontrolPush buttons generate an action when clickedpbh = 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]);
![Page 23: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/23.jpg)
uicontrolSliders 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]);
![Page 24: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/24.jpg)
uicontrolToggle 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]);
![Page 25: Matlab GUIs](https://reader035.fdocuments.us/reader035/viewer/2022081422/568165d2550346895dd8df70/html5/thumbnails/25.jpg)
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)