building a matlab gui - community.iisme.org
TRANSCRIPT
BUILDING A MATLAB GUI
Education Transfer Plan Seyyed Khandani, Ph.D.
IISME 2014
Seyyed Khandani/IISME/ETP/2014 [email protected] 2
Graphical User Interface (GUI)
A GUI is useful for presenting your final software.
It makes adjusting parameters and visualizing your
programs easier.
It allows wrapping the software in a form that could
be used by people with no knowledge of computer
programming.
It encourages design-oriented mentality which is
essential in engineering education.
MATLAB provide a tool, called guide, for creating a
GUI.
Typing guide in the command window opens the
“GUIDE Quick Start” window shown below:
>> guide
Begin by using
Blank GUI (default)
template.
3
Guide
Seyyed Khandani/IISME/ETP/2014 [email protected]
Guide window and blank GUI panel shown below
The window has 3 areas:
(1) The menu and menu
bar on the top.
(2) Buttons to select GUI
objects, or controls, on
the left.
(3) The resizable panel
with grid marks where
GUI objects will be
placed.
________________ To resize the panel grab the lower right corner with the mouse and stretch.
4
Guide Window
Seyyed Khandani/IISME/ETP/2014 [email protected]
Guide GUI objects are instances of a MATLAB class called uicontrol (user interface control)
Push button ------ A button to activate by mouse
Radio button ----- Changes state from unselected
to selected and back
Edit textbox ------ Input data by typing text into
the box
Pop-up menu ---- A list of options to select one
Toggle button ---- Pressed down, it stays down
until it is pressed again
Axes ----------------- A surface on which to display
two and three-dimensional
graphs
Button group ---- Groups a set of radio buttons
or toggle buttons; only one at
a time is selected
Slide ---------------- Enter a real number by adjusting
the position of the slider
Checkbox --------- Changes state between select and unselected
Static text --------- Displays a text; use for label or show the result of a calculation
Listbox ------------- Display a list of options where several can be selected
Table ---------------- Display data in a tabular form
Panel --------------- Groups objects together visually
ActiveX control For insertion of an ActiveX control made by another program
5
GUI Objects (controls) in Guide
Push button
Radio button
Edit textbox
Pup-up menu
Toggle button
Axes
Button group ActiveX control
Panel
Table
Listbox
Static text
Checkbox
Slide
Seyyed Khandani/IISME/ETP/2014 [email protected]
MyGUI is an example of how to create a GUI.
First layout the basic controls for our program.
It contains Axes(axes1),
Static Text, Edit Text,
and Push Button
controls.
Use drag-and-drop to
add GUI objects, or
controls, to the panel.
Resize and move around
each object to look like
the image on the right.
6
MyGUI
Selected Object
Tag Property Mouse Pointer
Position
Selected Object
Position
Resize Box
Seyyed Khandani/IISME/ETP/2014 [email protected]
Each GUI object has a number of properties. Double-
click on an object to open the property
inspector
Sample of properties (1) Tag – the name of the control in the code;
best to rename it to something identifiable like
“PlotButton” versus “button1”
(2) String – the text that appears on the control
(3) ForegroundColor – color of the text
(4) BackgroundColor – color of the control
(5) Value – current value (initial value is zero)
(6) Min and Max – depends on the type of control.
In slider, for example, they show the range of
the value
7
GUI Object Property Inspector
Seyyed Khandani/IISME/ETP/2014 [email protected]
Change properties of different controls: (1) axes1
tag: PlotArea
(2) push button
tag: PlotButton
string: Plot (3) edit text tag: EquationBox
string: Equation
(4) static text
tag: EquationLabel string: y=
Save the setup as MyGUI and then run it by pressing
on the menu bar. It will look like the image show above
It looks right but it does not do anything until callbacks
are defined. 8
Running MyGUI
Seyyed Khandani/IISME/ETP/2014 [email protected]
When MyGUI was saved, two files were created.
(1) MyGUI.fig – contains information about the GUI
layout, geometry, and its controls.
(2) MyGUI.m – contains the program that creates and
executes the GUI tool you have made.
– also contains code that defines a
callback for each of the controls in GUI.
– This m-file loads in the MATLAB editor
when MyGUI is first saved.
Generally the content of the m-file is left alone except for
some callback functions.
In the m-file, we will modify a callback function called
PlotButton_callback.
9
GUI File Formats
Seyyed Khandani/IISME/ETP/2014 [email protected]
10
MyGUI.m File Content
MyGUI.m contains a number of functions including:
(1) function MyGUI_OpeningFcn(hObject, eventdata, handles, varargin)
- This opening function, executes just before MyGUI is made visible.
- The function has no output.
- Input argument “handles” is a structure containing handles for all GUI
controls such as handles.EquationBox, and handles.PlotArea. - This function will not be modified
(2) function PlotButton_Callback(hObject, eventdata, handles)
- This is the callback function for plot button.
- The function has no output.
- Entering an equation in the Equation box followed by a click on the
Plot button, should result in display of the plot of that function in the
plot area. Therefore this function must be modified.
Seyyed Khandani/IISME/ETP/2014 [email protected]
11
Writing Callbacks
Since the only part to be modified is PlotButton_Callback
function. Then add the following code to the end of this
function % added code to MyGUI.m x=-10:0.1:10; % an arbitrary range for x s=get(handles.EquationBox, 'String'); % gets the equation entered y=eval(s); % eval function finds values of y h=handles.PlotArea; % records the handle of where % the plot is displayed plot(h,x,y); % plots on the PlotArea xlabel(h,'x-value'); ylabel(h,'y-value'); title(h,'Plot of y versus x');
Save MyGUI.m once
Seyyed Khandani/IISME/ETP/2014 [email protected]
12
Running MyGUI
When the m-file code is modified, there is no need to re-
run the GUI. The new code will run as a callback
allowing a quick testing of the changes made.
To run the GUI, the only thing to do is to enter MyGUI in
the MATLAB command window:
>> MyGUI
As a result of running MyGUI.m, the corresponding GUI
opens. First enter your function in the Equation box and
then click on the Plot button to see the results.
Seyyed Khandani/IISME/ETP/2014 [email protected]
13
Test Results for MyGUI
Inputs: y=sin(x), or y=4*x.^3+2*x-1, or y=x.*sin(x)
Seyyed Khandani/IISME/ETP/2014 [email protected]
14
Usefull MATLAB Functions
get This function is used to get the properties of a graphic (e.g. gui) object.
<var>=get(handle, ‘PropertyName’)
where handle is a handle to the object and var represent the value of the
property listed
set This function sets the value of a property of a graphic (e.g. gui) object.
set(handle, ‘PropertyName’, property_value)
findobj Finds objects with specified property values.
handles = findobj('P1Name',P1Value,...)
returns the handles of all objects whose property values match those
passed as param-value pairs to the findobj command.
Seyyed Khandani/IISME/ETP/2014 [email protected]