Sei sulla pagina 1di 8

Feedback Control Systems Lab

Eng. Tareq Abu Aisha


Lab # 9


Islamic University of Gaza
Faculty of Engineering
Computer Engineering Dep.

Lab # 9 Introduction to MATLAB Graphical User Interface (GUI)
Introduction:
1) What is a GUI?
A graphical user interface (GUI) is a graphical display that contains devices, or
components, that enable a user to perform interactive tasks. To perform these tasks,
the user of the GUI does not have to create a script or type commands at the
command line. Often, the user does not have to know the details of the task at hand.
The GUI components can be menus, toolbars, push buttons, radio buttons, list boxes,
and sliders just to name a few. In MATLAB, a GUI can also display data in
tabular form or as plots, and can group related components.

The following figure illustrates a simple GUI



The GUI contains
An axes component.
Four edit texts.
Five static texts.
Six buttons.





2) How Does a GUI Work?
Each component, and the GUI itself, is associated with one or more User-written
routines known as callbacks. The execution of each callback is triggered by a
particular user action such as a button push, mouse click, selection of a menu item,
or the cursor passing over a component. You, as the creator of the GUI, provide
these callbacks.

3) Where Do I Start?
You have to design your GUI and then draw it on paper.
Then you have to decide what you want it to do, how you want the user to
interact with it, and what components you need.
Then you have to lay out your GUI.
Next, you must decide what technique you want to use to create your GUI.
MATLAB enables you to create GUIs programmatically or with GUIDE, an
interactive GUI builder. The technique you choose depends on your
experience, your preferences, and the kind of GUI you want to create.
After that you will start programming your GUI.

4) What is GUIDE?
GUIDE, the MATLAB graphical user interface development environment, provides
a set of tools for creating graphical user interfaces (GUIs). These tools simplify the
process of lying out and programming GUIs.

5) Programming the GUI
When you save your GUI layout, GUIDE automatically generates an M-file that you
can use to control how the GUI works. This M-file provides code to initialize the
GUI and contains a framework for the GUI callbacks the routines that execute in
response to user-generated events such as a mouse click. Using the M-file editor,
you can add code to the callbacks to perform the functions you want.

Programming GUI Components

Push Button
This example contains only a push button. Clicking the button closes the GUI.



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)
display Goodbye
close(handles.figure1);



Edit Text
To obtain the string a user types in an edit box, get the String property in the
callback.
a) To get data from edit text we use the following code:


function edittext1_Callback(hObject, eventdata, handles)
user_string = get(hObject,'String');
user_data = str2double(user_string);
% Proceed with callback


b) To set data to edit text we use the following code:


set(hObject,'String',['The result is: ',num2str(result)])
% Proceed with callback


Slider
You can determine the current value of a slider from within its callback by querying
its value property, as illustrated in the following example:


function slider1_Callback(hObject, eventdata, handles)
slider_value = get(hObject,'Value');
% Proceed with callback


The Max and Min properties specify the slider!s maximum and minimum values.
The slider!s range is Max - Min.

List Box
When the list box callback is triggered, the list box value property contains the index
of the selected item, where 1 corresponds to the first item in the list. The string
property contains the list as a cell array of strings.
This example retrieves the selected string. It assumes listbox1 is the value of the tag
property. Note that it is necessary to convert the value returned from the string
property from a cell array to a string.


function listbox1_Callback(hObject, eventdata, handles)
index_selected = get(hObject,'Value');
list = get(hObject,'String');
item_selected = list{index_selected}; % Convert from cell array to string
% Proceed with callback


Pop-Up Menu
When the pop-up menu callback is triggered, the pop-up menu value property
contains the index of the selected item, where 1 corresponds to the first item on the
menu. The string property contains the menu items as a cell array of strings.



function popupmenu1_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
switch val
case 1
% User selected the first item
case 2
% User selected the second item
% Proceed with callback


Axes
Axes components enable your GUI to display graphics, such as graphs and images.

Plotting to a GUI with a Single Axes
If a GUI contains only one axes, MATLAB automatically generates the plot in that
axes.

Plotting to a GUI with Multiple Axes
If a GUI has multiple axes, you should explicitly specify which axes you want to
target. You can make the axes you want to target the current axes by using the axes
function. In the following command, axes1 is the tag property of the target axes.
axes(handles.axes1)

Sharing Data among a GUIs Callbacks
You can use GUI data to share data among a GUI!s callbacks. This topic shows you
how to do this:

GUI Data
GUI data, which you manage with the guidata function, is accessible to all callbacks
of the GUI. A callback for one component can set a value in GUI data, which can
then be read by a callback for another component.


handles.data = X;
guidata(hObject,handles);
% Save X in data.

%To be used in another callback
Y = handles.data;
%now Y(in some callback) = X(in another callback)

Note:

If we are writing the code in the callback of the current object we use hObject to
deal with the object, else we use handles.Object_Name to deal with the object.







Example:
Build a MATALB GUI project to work as the following:
The user must enter the open loop transfer functions G(s) as num and den.
The GUI will find the closed loop transfer function with a unity feedback as
num and den and then will show the result to the user.
The GUI will be able to draw both the step and impulse responses of the
closed loop transfer function.
The GUI will be able to find the steady state error and display the result to
the user.
Show the figure of your closed loop system to identify it to the user.
From "Toolbar Editor" add data cursor so that we can compare the
mathematical result with the graph result.
Your GUI must give a help for a new user if he/she needs a help.
The user must be able to exit from the GUI using a push button.




Solution:
1) Write guide in the MATLAB command window to open the GUI development
environment and then from Create New GUI choose Blank GUI (Default).
2) Make your Project look like the following project:



3) By double clicking on each component you will notice that it has many
properties; among them we are interested in the following:
Background Color: to change the color of the background of any
component.
Foreground Color: to change the color of the string shown on any
component.

String: to change the name shown to the user like "Closed Loop TF is"
and "Steady State Error" etc.
Tag: to change the programming name in the M-file for any component;
this name is not shown to the user.
Max, Min: to change the number of lines in edit text components; the
default is one line (Max = Min = 1);
For example if Max = 3 and Min = 1, then the edit text contains three
lines.
Now change the String names to be as the names shown, and try to pick
meaningful names in the tag field.
4) After that save your project as "Hello_project". You will notice that the
MATLAB makes an M-file for your figure.
5) The code written by MATLAB is the required code to identify your components
to MATLAB. For every component except the axis you will notice that there are
two parts of the code:
ComponentName_Callback: here you will write some code to make this
component do some actions.
ComponentName_CreatFcn: you will not write any code here, this code
is written by MATLAB.
6) To show the graph of the closed loop system to the user we will write its code in
the main function callback, since this graph will be shown as soon as the project
is run independently of any other action.
In Hello_project_OutputFcn write the following code:


X = imread('ClosedLoop.jpg');
axes(handles.axes5);
imshow(X)
axis off


7) In the push button whose string is "Closed Loop TF is:" write the following
code:


num = str2num(get(handles.num,'String'));
den = str2num(get(handles.den,'String'));
X = tf(num,den);
[n,d] = feedback(num,den,1,1);
Y = tf(n,d);
handles.X = X;
handles.Y = Y;
handles.n = num;
handles.d = den;
guidata(hObject,handles);
set(handles.TF,'String',['Your TF is: ',sprintf('\n'),'num ',num2str(n),sprintf('\n'),'den
',num2str(d)])


8) In the push button whose string is "Steady State Error" write the following code:





J = 0;
L = 0;
syms x;
n = handles.n;
d = handles.d;
for i = 1:length(n)
J = J+n(i)*x^(length(n)-i);
end
for j = 1:length(d)
L = L+d(j)*x^(length(d)-j);
end
O = (J/L);
p = subs(((1)/(1+O)),0)
set(handles.KKK,'String',['SSE is: ',num2str(p)])


9) In the push button whose string is "StepResponse" write the following code:


Y = handles.Y;
axes(handles.axes4)
step(Y);
grid on


10) In the push button whose string is "ImpulseResponse" write the following code:


M = handles.Y;
axes(handles.axes4)
impulse(M);
grid on


11) In the push button whose string is "Exit" write the following code:


close all


12) In the push button whose string is "Help" write the following code:


HelpPath = which('hello_help.html');
web(HelpPath);







13) Now run the project and enter the values as shown:



14) If you press help then the following figure will appear to you:



Exercise:
Try to pick one project from the suggested projects on my Web Page and start doing
it so that you submit it on time.




For more information about creating GUI's download the following book:
http://www.mediafire.com/?qyttwdky110

Potrebbero piacerti anche