Introduction To The Graphical User Interface (Gui) in Matlab
Introduction To The Graphical User Interface (Gui) in Matlab
GRAPHICAL USER
INTERFACE (GUI) IN MATLAB
Video of the Week
https://
www.youtube.com/watch?v=wSnx4V_RewE
What is a GUI?
Graphical User Interface
What?
A pictorial based interface that uses menus, buttons, the
mouse, and other “graphics” to communicate with the user.
[No command line interaction]
Examples:
The Windows Calculator
Firefox, Thunderbird, Office
Anything you are looking at [on the computer].
What is a GUI in MATLAB?
GUI’s in MATLAB consist of two files:
An “m-file” [******.m]
A “fig-file” [******.fig]
The m-file has all of the code that controls the GUI
The fig-file has all of the graphical objects, positions,
default values, and links it all together
MATLAB GUI -- Example
Edit Text
Boxes Panel
Axes
Pop Up-menu
Radio
Button
Sliders
to Change figure.
1
Chapman, Stephen J. MATLAB Programming for Engineers. 2005
Handle Graphics
Each component has a list of properties that define
what it looks like and how it behaves.
Plot Something:
Figure Window View Property Editor
More Properties
Handle Graphics
Handle Graphics
The manipulation of these properties form the basis
of GUIs and GUI programming in MATLAB.
The ‘handle’
>> fh = figure();
>> set(fh,'Name','Figure: Meet the World!');
>> set(fh,'NumberTitle','off');
>> ph = plot([1:10],[1:10].^2);
>> set(ph,'LineStyle','--');
>> set(ph,'Marker','square');
>> set(ph,'MarkerEdgeColor',[1 0 0],
'MarkerFaceColor',[0 1 0]);
>> get(ph); get(fh); %Look at all properties
Handle Graphics
The ‘set’ and ‘get’ commands
These are the primary commands that you use to … set
and get information about graphic objects, they update
the graphical object immediately
Syntax: ‘set’
>> set(object_hndl,'PropertyName',propvalue);
Syntax: ‘get’
>> propvalue = get(object_hndl,'PropertyName');
Creating a GUI
Step 1: Create the graphical components
Using the GUIDE
Tab Order Editor Set the tab and stacking order of the components in your layout.
Property Inspector Set the properties of the components in your layout. It provides a
list of all the properties you can set and displays their current
values.
Object Browser Display a hierarchical list of the objects in the GUI.
M-File Editor Display, in your default editor, the M-file associated with the GUI.
Step 1: The GUI – The Guide
MATLAB’s help files are going to help you the
most.
Search for:
GUIDE: Tools Summary
Previous two slides plus more details on each part
Creating a GUI with GUIDE
Step by step how to create a simple GUI
Step 1: Creating the GUI - [fig file]
Place Components
Figure out what you want.
Inputs
Outputs
Parameters / Configuration Options?
Choose appropriate components
See List
Place the objects
Click on icon [from component palette]
Click and drag in Layout Area
Configure the component
Double Click on component or right click and click on Property Inspector.
Change attributes
Align components
Step 1: Complete
We have placed our components and now:
Press save
Name your file
Up pops an m-file
Notice there is a lot of code
There is even more pseudocode
[This is a good thing]
Let’s take a closer look
Step 2: Creating the GUI – [m file]
Programming the GUI
GUI Files: An Overview
GUI M-File Structure
Callbacks: An Overview
Callback Input Arguments
Adding Callbacks to GUI M-File
Useful Commands
Examples of GUI Components
Step 2: Programming the GUI
Section Description
Comments Displayed at the command line in response to the help
command. Edit these as necessary for your GUI.
Opening Performs your initialization tasks before the user has access
function to the GUI.
Output Returns outputs to the MATLAB command line after the
function opening function returns control and before control returns to
the command line.
Component Control the behavior of the GUI figure and of individual
and figure components. MATLAB calls a callback in response to a
callbacks particular event for a component or for the figure itself.
Step 2: Programming the GUI
Callbacks: An Overview
What is a Callback
A function associated with a GUI component. It controls the
behavior by performing an action in response to an event.
Kinds of Callbacks
Table
Step 2: Programming the GUI
Callback Syntax and Arguments
Most callbacks will look similar to this:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
...
name
Input Arguments
hObject Handle of the object, e.g., the component
eventdata Reserved for later use.
handles Structure that contains the handles of all the objects in the figure. It
may also contain application-defined data.
Step 2: Programming the GUI
Adding Callbacks
The GUIDE only creates the most common callbacks
You may want to create different ones:
Right-click on the component you want
You are now looking at the Layout Editor context menu*
Select View callbacks
Select the callback you wish to create
The GUIDE will now add it to the m-file and open it.
Step 2: Programming the GUI
There are two key functions:
set
sets values
get
gets current values
Step 2: Programming the GUI
the get function
We have seen many examples:
user_data = get(hObject,’String’);
returns string in hObject
user_data = get(hObject,’Value’);
returns value in hObject
user_data = get(hObject,’max’);
returns max possible value in hObject*
same for min
Please note:
These commands only work when you are inside the callback function
you are trying to “get” the value of.
Step 2: Programming the GUI
the get function
In general:
To access data stored in a component somewhere else in your
program:
user_data = get(handles.component_name,’Value’)
set(handles.component_name,’propertyname’,valuetoset)
This is the push button's Callback. It displays the string Goodbye at the
command line and then closes the GUI.
function pushbutton1_Callback(hObject, eventdata, handles)
disp(‘Goodbye’)
delete(handles.figure1);
Programming the GUI
Components
Toggle Button
The callback for a toggle button needs to query the toggle button to determine
what state it is in. MATLAB sets the Value property equal to the Max property
when the toggle button is pressed (Max is 1 by default) and equal to the Min
property when the toggle button is not pressed (Min is 0 by default).
The following code illustrates how to program the callback in the GUI M-file.
Note: To set Resize behavior for the figure to Other (Use ResizeFcn), select
GUI Options from the Layout Editor Tools menu.
Programming the GUI
Components
Button Group
Button groups are like panels except that they manage exclusive selection
behavior for radio buttons and toggle buttons. If a button group contains a set
of radio buttons, toggle buttons, or both, the button group allows only one of
them to be selected. When a user clicks a button, that button is selected and all
others are deselected.
The following figure shows a button group with two radio buttons and two toggle
buttons. Radio Button 1 is selected.
Programming the GUI
Components
Button Group (cont.)
If a user clicks the other radio button or one of the toggle buttons, it becomes selected
and Radio Button 1 is deselected. The following figure shows the result of clicking
Toggle Button 2.