0% found this document useful (0 votes)
8 views33 pages

MATLAB GUI Development Tutorial

Elektronik mh

Uploaded by

67wmt2r58v
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views33 pages

MATLAB GUI Development Tutorial

Elektronik mh

Uploaded by

67wmt2r58v
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GUI Development Environment

(guide)

If you haven’t already done so, download the


ProjectTutorialCode.m file and put it in you current
MATLAB folder!
GUI Development Environment
(guide)
 At the command prompt in MATLAB, type
>> guide

 Select the option to


Create New GUI,
Blank GUI (Default)
Add Objects

 Drag and drop


components
from the menu of
icons to create
this GUI:
Set up Properties of Objects
 Right click in the GUI block (not on any specific object) and
select Property Inspector. Pick a name for the GUI like
FirstGui. Leave the property inspector open.

 Click on the top Edit Text block. In the property inspector


for this box: set the String to 1 (the number one) and set
the Tag to FreqBox.

Caution: Make sure you make the tag names exactly the
same as what is specified. Otherwise the code you are
going to copy and paste won’t run!
Set up Properties of Objects
 Click on the top static text block and set the String to
Frequency (Hz).

 Click on the slider and set the Value to 5 (do this by clicking on
the little icon next to Value then clicking on the 0.0 and
overwriting it with 5 (don’t Append). Set the Max to 10. Set the
Tag for the slider to AmpSlider.

 Click on the Edit Text box under the slider. In the property
inspector, set the String to 5 and the Tag to AmpBox.

 Click on the static text block next to the slider and make the
string Amplitude.
Set up Properties of Objects
 Click on the static text block above the pop up menu and
set the String to Select a Graph.

 Click on the pop up menu block and set the Tag to


GraphMenu and set the String to the following:
cos(2*pi*t)
tan(t) Make sure cos(2*pi*t) is on the
very first line – don’t leave a
exp(-t) blank line at the top.
exp(t)
ln(t)
Your GUI should now look like this:
Activate Your GUI
 Click on the Green Play arrow to activate the GUI.
 Save the GUI under some name (FirstGui). Note: name
must follow same rules as naming variables in MATLAB!
 An m-file will automatically open (with the same name)
 The GUI which currently does almost nothing at this point
will launch.
 The m-file has callback functions where code can be added
to control what the various objects (pushbuttons, drop down
menus, plots, etc) will do. Any executable MATLAB code
can be placed within these functions.
Activate Your GUI
At this point, your GUI does almost nothing.

 You can move the slider back and forth


Caution: don’t click really fast on the arrows next to the slider
or MATLAB will lock up!

 You can select something from the pop-up menu

 You can write stuff in the edit boxes


Writing Code for Components
If you click on
the blue Go To
Arrow, you get
a list of
functions
already in your
program
corresponding
to your
components.

Clicking on
one of the
functions on
this list takes
you directly to
that function
(better than
scrolling!)
Opening Function
1. Copy lines 8-18 from the
ProjectTutorialCode.m file.

2. Go back to the
FirstGui.m file, click on the
Go To Arrow and select
FirstGui_OpeningFcn

3. Paste the code you just


copied right below the first
five lines of comments but
before the first executable
command that is already in
the opening function
Opening Function
Hit the Green Arrow to Save Your Changes and launch the GUI again.
Now you should have something in your two plot windows:
Opening Function
%The Opening Function Sets up how FirstGui looks when first
opened

axes(handles.axes1); % Point to the top plot tagged as axes1


% Plot a 1 Hz sine wave with amplitude of 5
t = 0:1e-6:1; y = 5*sin(2*pi*t);
plot(t,y); axis([0 1 -10 10]);

axes(handles.axes2); % Point to the bottom plot tagged as axes2


% Plot a 1 Hz cosine wave with amplitude of 1
t = -1:0.001:1; y = cos(2*pi*t); plot(t,y);
title('cos(2*pi*t)');
Pop-Up Menu (GraphMenu)
1. Copy lines 25-42 from
the ProjectTutorialCode.m
file.

2. Go back to the
FirstGui.m file, click on the
Go To Arrow and select
GraphMenu_Callback

3. Paste the code you just


copied right below the
Hints in the
GraphMenu_Callback
function

Caution: don’t accidently


paste in
GraphMenu_CreateFcn
Pop-Up Menu
Hit the Green Arrow to Save Your Changes and launch the GUI again.
Select different items from the pop-up menu and see graph change!
Pop-Up Menu
axes(handles.axes2); % Makes axes2 the selected plot window
cla; % Clears axes2

menu_choice = get([Link], 'Value');


% Get user choice from pop-up menu tagged as menu1
switch menu_choice
case 1
t = -1:0.001:1; y = cos(2*pi*t); plot(t,y);
title('cos(2*pi*t)');
case 2
t = -2*pi:0.001:2*pi; y = tan(t);
plot(t,y); axis([-2*pi 2*pi -10 10]); title('tan(t)');
case 3
t = 0:0.001:5; y = exp(-t); plot(t,y); title('exp(-t)');
case 4
t = -5:0.001:3; y = exp(t); plot(t,y); title('exp(t)');
case 5
t = 0.1:0.001:5; y = log(t); plot(t,y); title('ln(t)');
end
Edit Box (FreqBox)
1. Copy lines 49-68 from
the ProjectTutorialCode.m
file.

2. Go back to the
FirstGui.m file, click on the
Go To Arrow and select
FreqBox_Callback

3. Paste the code you just


copied right below the
Hints in the
FreqBox_Callback

Caution: don’t accidently


paste in
FreqBox_CreateFcn
Edit Box (FreqBox)
Hit the Green Arrow to Save Your Changes and launch the GUI again.
Type different values in the FreqBox Edit Box and see the top graph change
Edit Box (FreqBox)
freq = str2double(get([Link],'String'));
% Get the frequency from box tagged FreqBox
if isnan(freq)
% If freq isn't a number over-write it with a 1
freq = 1;
set([Link], 'String',1); % Write a 1 into the box
errordlg('Input must be a number');
% Tell user to put in a number
pause(2);
end

amp = get([Link],'Value'); % Get amplitude from


Slider tagged AmpSlider

axes(handles.axes1); %Select axes1 for plotting


cla; %Clear out axes 1

t = 0:1e-6:1;
y = amp*sin(2*pi*freq*t);
plot(t,y); axis([0 1 -10 10]);
Slider (AmpSlider)
1. Copy lines 75-86 from
the ProjectTutorialCode.m
file.

2. Go back to the
FirstGui.m file, click on the
Go To Arrow and select
AmpSlider_Callback

3. Paste the code you just


copied right below the
Hints in the
AmpSlider_Callback

Caution: don’t accidently


paste in
AmpSlider_CreateFcn
Slider (AmpSlider)
Hit the Green Arrow to Save Your Changes and launch the GUI again.
Move the slider and see the amplitude of the sin wave change and the value
in the box change. Caution: don’t click really fast on the arrows next to the
slider or MATLAB will lock up!
Slider (AmpSlider)
amp = get([Link], 'Value'); % Get the new slider value

freq = str2double(get([Link],'String'));
% Get frequency from box tagged FreqBox

axes(handles.axes1); %Select axes1 for plotting


cla; %Clear out axes 1

t = 0:1e-6:1;
y = amp*sin(2*pi*freq*t);
plot(t,y); axis([0 1 -10 10]);

set([Link], 'String',amp);
% Writes value of slider into box tagged AmpBox
Edit Box (AmpBox)
1. Copy lines 93-110 from
the ProjectTutorialCode.m
file.

2. Go back to the
FirstGui.m file, click on the
Go To Arrow and select
AmpBox_Callback

3. Paste the code you just


copied right below the
Hints in the
AmpBox_Callback

Caution: don’t accidently


paste in
AmpBox_CreateFcn
Edit Box (AmpBox)
Hit the Green Arrow to Save Your Changes and launch the GUI again.
Enter different amplitudes in the Amplitude Box and the slider will move as
long as the amplitude ranges from 0 to 10.
Edit Box (AmpBox)
amp = str2double(get([Link],'String'));
% Retrieve what was entered in box
if isnan(amp) || amp < 0 || amp > 10
amp = get([Link],'Value');
% Get Current Slider Value and write it into AmpBox
set([Link],'String',amp);
errordlg('Input Must be a Number from 0 to 10')
pause(2)
end

set([Link],'Value',amp) % Move Slider to value

freq = str2double(get([Link],'String'));

axes(handles.axes1); %Select axes1 for plotting


cla; %Clear out axes 1

t = 0:1e-6:1;
y = amp*sin(2*pi*freq*t);
plot(t,y); axis([0 1 -10 10]);
What is handles?
 handles is a structure that has contains all of the
properties of all of the objects added to your GUI.

 Think of handles as being similar to a dataset array. In


our dataset array of weather data, CVG, we referenced
the maximum daily temperature column as [Link].
In a GUI, we refer to an object as [Link]
Programming a GUI: Key Commands
 The get command allows you to retrieve any property of
any component in your GUI.

 The set command allows you to set any property of any


component in your GUI.

 The PowerPoint, Guide to GUIs, posted in the recitation


folder on the metasite under Team Project has details on
key properties for each component.
Common Errors
The usual syntax errors and spelling errors that
create problems in writing any program. Learn to
spell handles!
Using the same name for a tag and for a variable.
For example, if you have an edit box that has a tag,
Amplitude, don’t use Amplitude as a variable name
in your program.
Don’t end the individual callback functions. If you
do, all functions underneath won’t be recognized –
you’ll get a red line under all remaining functions. Of
course, you do have to include end statements for all
conditional statements and loops.
Common Errors
 Double clicking on the .fig file will not activate your
GUI. It will look OK but when you try to do
something, you will get errors. You activate your
GUI by typing the name of the GUI .m file at the
command prompt or by opening the .m file and
clicking on the green activate arrow.
 If you decide for some reason to re-name your GUI,
you cannot do this by simply re-naming the .m and
.fig files. You must re-open your GUI using guide,
then do a save as with the new name. MATLAB will
create a new .m and .fig file with the new name. The
.m file will have all of your original code but is
adapted to the new name.
Common Errors
 In addition to a CallBack Function, each component
also generates a Create Function in your m-file.
Don’t delete these Create Functions. If you do
inadvertently delete one, simply go back to your GUI
layout (guide). Right click on the component, select
View Callbacks, then click on Create Function. This
will put the Create Function back in your m-file.
If you want to send a GUI to the members of your
team, you must send both the .fig and .m files You
will also need to do this when you submit your final
project.

You might also like