Matlab 5
Matlab 5
Lecture-5
Introduction to Graphic User
Interface (GUI)
Events
GUI based programs
are prepared for
mouse clicks (or
possibly keyboard
inputs) for any GUI
element at any time.
Such inputs are
known as events
and a program that
responds to events
is said to be event
driven.
GUI Composition:
The three principal elements required to create a
MATLAB GUI are:Components Containers Callbacks
Components
Each item on a MATLAB GUI (e.g.,
pushbuttons, labels, edit boxes) is a
graphical component.
The types of components include
graphical controls (pushbuttons,
toggle buttons, edit boxes, lists,
sliders, etc..), static elements
(text boxes), menus, toolbars &
axes.
Containers
The components of a GUI
must be arranged within a
container, which is a
window on the computer
screen.
The most common
container is the figure. It
has a title bar along the top
and can optionally have
menus attached.
Other Ex- Button Groups
and Panels.
Callbacks
There must be some way to perform
an action if a user clicks a mouse
on a button or types information
on a keyboard.
A mouse click or a key press is an
event and the MATLAB program
must respond to each event if the
program is to perform its function.
The code executed in response to an
event is known as a callback.
Launch guide:
drag and drop static text box and push button
Change tag and string of these components by
double clicking over component and from
property inspector changing its tag and string.
save the file with the name demo1
now open the MATALB file and run it.
b
u
Do
lic
c
e
It will
look like
Practice Exercise 1:
Figure should
look like
Write code in
callback of edit
box
Button
Push Button
Calls G
ui
pushb _Demo1 wit
h
u tton 1
_Callb argument
ack
Gui_Demo1
Gui_Demo1_Callback
Total Clicks:1
Push Button
Edit Boxes
It is a graphical object that allows a user to enter
one or more text strings. It is created by the
uicontrol.
If the min property and max property are both
set to 1, then the edit box will accept a single line
of text, and it will generate a callback when the
user presses the Enter key after typing the text.
If the max property is set to a number greater
than the min property, the edit box will accept as
many lines of text as the user wishes to and puts
a vertical scrollbar
Assignment-1
Create a simple Echo of an Edit Box on
the Static Text Box.
Write code in
callback of edit box
It should look like
Push Buttons
It is a component that a user can click on
to trigger a specific action. Push Buttons
retain their original click position after
clicked by the mouse.
It generates a callback when the user
clicks on it with the mouse.
Pushbutton is created by the uicontrol.
Toggle Buttons
It is a type of button that has two states:
on (depressed) & off (not depressed).
A toggle button switches between these
two states whenever the mouse clicks on
it and it generates a callback each time.
The Value property of the toggle button
is set to max (usually 1) when the button
is on and min (usually 0) when the button
is off.
Assignment-2
Write code in
callback of edit box
It should look like
Assignment-3
1.
2.
2.
Write code in
callback of
radio buttons
Popup Menu
These are graphical objects that allow a user
to select one of a mutually exclusive list of
options.
The list of options that the user can select
among is specified by a cell array of strings
and the Value property indicates which of
the strings is currently selected.
In guide to add more options in Popup Menu,
go to String option and press the icon to add
more elements to it.
Assignment-4
Demonstrate the use of Popup Menu by
displaying which option of popup menu is
selected on the screen.
function Popup1_Callback(hObject, eventdata,
handles)
value=get(handles.Popup1,'Value');
str = ['Option' num2str(value)];
set(handles.TextBox,'String',str);
List Box
These are graphical objects that displays many
lines of text and allow a user to select one or
more of those lines.
List boxes can be used to select a single item
from a selection of possible choices. In
normal GUI usage , a single mouse click on a
list item selects that item but does not cause
an action to occur.
Instead the action waits on some external trigger
like pushbutton. However, a double mouse
click causes an action to happen
immediately.
Explanation of gcbf
fig = gcbf returns the handle of the figure
that contains the object whose callback is
currently executing.
This object can be the figure itself, in
which case, gcbf returns the figure's
handle.
When no callback is executing,
gcbf returns the empty matrix, [].
Explanation of Invocation
If a selection is made in the list box, function
ListBox1_Callback will be executed. This function
will check the figure producing the callback (using
function gcbf) to see whether the selection made
was single or double click.
For a single click it does nothing. For a double click,
the function gets the selected value from the list box
and writes an appropriate string into the text field.
If the pushbutton is selected, the callback function is
accordingly executed. The function gets the selected
value from the listbox and writes an appropriate
string into the text field.
Sliders
These are graphical objects that allow a
user to select values from a continuous
range between a specified minimum value
and a specified maximum value by moving
a bar with a mouse.
The Min and Max fields need to be set for
slider to show the min. & Max. values.
Assignment-5