lab 91

Upload: abrahamdqhaus

Post on 07-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Lab 91

    1/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, orcomponents, 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 thecommand 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 containsAn axes component.

    Four edit texts.Five static texts.

    Six buttons.

  • 8/3/2019 Lab 91

    2/8

    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, providethese 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, providesa set of tools for creating graphical user interfaces (GUIs). These tools simplify the

    process of lying out and programming GUIs.

    5) Programming the GUIWhen you save your GUI layout, GUIDE automatically generates an M-file that youcan 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 inresponse 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 ButtonThis example contains only a push button. Clicking the button closes the GUI.

    This is the push buttons 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);

  • 8/3/2019 Lab 91

    3/8

    Edit TextTo 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

    SliderYou can determine the current value of a slider from within its callback by queryingits 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 sliders maximum and minimum values.

    The sliders range is Max - Min.

    List BoxWhen 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 MenuWhen 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 themenu. The string property contains the menu items as a cell array of strings.

  • 8/3/2019 Lab 91

    4/8

    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 AxesIf a GUI contains only one axes, MATLAB automatically generates the plot in that

    axes.

    Plotting to a GUI with Multiple AxesIf 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 CallbacksYou can use GUI data to share data among a GUIs callbacks. This topic shows you

    how to do this:

    GUI DataGUI 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 canthen be read by a callback for another component.

    handles.data = X;

    guidata(hObject,handles);

    % Save X in data.

    %To be used in another callbackY = 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.

  • 8/3/2019 Lab 91

    5/8

    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 themathematical 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 developmentenvironment 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 manyproperties; 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.

  • 8/3/2019 Lab 91

    6/8

    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; thedefault is one line (Max = Min = 1);

    For example if Max = 3 and Min = 1, then the edit text contains threelines.

    Now change the String names to be as the names shown, and try to pickmeaningful names in the tag field.

    4) After that save your project as "Hello_project". You will notice that theMATLAB makes an M-file for your figure.

    5) The code written by MATLAB is the required code to identify your componentsto 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 inthe 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 followingcode:

    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:

  • 8/3/2019 Lab 91

    7/8

    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);

  • 8/3/2019 Lab 91

    8/8

    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 doingit so that you submit it on time.

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

    http://www.mediafire.com/?qyttwdky110http://www.mediafire.com/?qyttwdky110