Top Banner

of 39

Basic Matlab GUI

Apr 14, 2018

Download

Documents

Farhan Babar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/30/2019 Basic Matlab GUI

    1/39

    I - 1Building a Graphical User Interface with MATLAB

    Basic Matlab

    Matr ix defini t ion

    a=[1,2 ; 3,4] => a= 1 2

    3 4

    Vector is a matrix with one column or one row.

    a=[1;2;3;4;5 ;; ] one column

    b=[1,2,3,4,5,6 ,,..] one row

  • 7/30/2019 Basic Matlab GUI

    2/39

    I - 2Building a Graphical User Interface with MATLAB

    Basic Matlab

    Example

    a=[1,2,3,4,5];

    b=[a;a;a]

    b= 1 2 3 4 5

    1 2 3 4 5

    1 2 3 4 5

    Example

    a=[1;2;3;4;5];

    b=[a,a,a]

    b= 1 1 1 1

    2 2 2 2

    3 3 3 3

    4 4 4 45 5 5 5Examp le (transpose)

    a=[1;2;3;4;5];

    b=a

    b= 1 2 3 4 5

  • 7/30/2019 Basic Matlab GUI

    3/39

    I - 3Building a Graphical User Interface with MATLAB

    Basic Matlab

    Str ings

    a=[a,b,c]

    a= abcb=[a;a;a]

    b= abc

    abcabc

    Str ings

    a=[a,b,c]

    a= abcb=[a,a,a]

    b= abcabcabc

    Str ings

    a=[a,b,c]

    b=[a,c,c]c=(a==b);

    c= 1 0 1

  • 7/30/2019 Basic Matlab GUI

    4/39

    I - 4Building a Graphical User Interface with MATLAB

    Basic Matlab

    1 2

    3 4

    5 67 8

    6 8

    10 12

    19 22*

    43 50

    A

    B

    A B

    A B

    5 12. *

    21 32

    1 4. ^ 29 16

    sin(1) sin(2)

    sin( ) sin(3) sin(4)

    A B

    A

    A

  • 7/30/2019 Basic Matlab GUI

    5/39

    I - 5Building a Graphical User Interface with MATLAB

    Basic Matlab

    A=zeros(a1, a2, a3, an);

    A is an n dimensional matrix of zeros.

    A=ones(a1, a2, a3, an);A is an n dimensional matrix of ones.

    size(A) return the size of A at each diminution.

    size(A,Dim) return the size of A at the diminution Dim.

  • 7/30/2019 Basic Matlab GUI

    6/39

    I - 6Building a Graphical User Interface with MATLAB

    Basic Matlab

    A(m,n) returns the value of the matrix in row-m

    and column-n.

    b=A(1:end,1) : b will be equal to column 1 of A

    b=A(5:10,5:10) : b will a-6x6 matrix containing all

    values of A from rows 5-10 and columns 5-10.

  • 7/30/2019 Basic Matlab GUI

    7/39

    I - 7Building a Graphical User Interface with MATLAB

    Basic Matlab

    Functions in Matlab

    function [output variables]=function_name (input variables)

    Input and output variables, can be of any type.

  • 7/30/2019 Basic Matlab GUI

    8/39

    I - 8Building a Graphical User Interface with MATLAB

    function [out_1,out_2,out_3] = Function_Dec (in_1,in_2,in_3)

    out_1=in_1+in_2+in_3;out_2=[ 'hello' ; 'world' ];

    out_3=[1,2,3,4,5];

    return;

    Basic Matlab

    Functions in Matlab

  • 7/30/2019 Basic Matlab GUI

    9/39

    I - 9Building a Graphical User Interface with MATLAB

    input:

    [a,b,c]=Function_dec(5,3,2)

    output:

    a = 10

    b = hello

    world

    c = 1 2 3 4 5

    input:

    [a,b,c]=Function_dec([1,2,3],[6,5,4],[3,4,5])

    output:

    a = 10 11 12

    b = hello

    world

    c = 1 2 3 4 5

    Basic Matlab

    Functions in Matlab

  • 7/30/2019 Basic Matlab GUI

    10/39

    I - 10Building a Graphical User Interface with MATLAB

    Basic Matlab

    Bit-wise operationsVariables must be integers

    BITAND (a,b) Bit-wise AND.

    BITOR (a,b) Bit-wise OR.

    BITXOR (a,b) Bit-wise XOR.BITGET (a,bit-num) Get bit.

    BITSET (a,bit-num,1/0) Set bit.

    BITSHIFT (a,+/- shift_size) Bit-wise shift.

  • 7/30/2019 Basic Matlab GUI

    11/39

    I - 11Building a Graphical User Interface with MATLAB

    Basic Matlab

    conditionsIf ( Boolean expression)

    .

    end;

    Boolean exp ression== : Is Equal

    ~=: Not Equal

    > : Is grater then< : Is less Then

    >=: Is grater then or equal to

  • 7/30/2019 Basic Matlab GUI

    12/39

    I - 12Building a Graphical User Interface with MATLAB

    Basic Matlab

    conditionsswitch switch_expr

    case case_expr,statement, ..., statement

    case {case_expr1, case_expr2, case_expr3,...}statement, ..., statement

    ...

    otherwise,statement, ..., statement

    end

  • 7/30/2019 Basic Matlab GUI

    13/39

    I - 13Building a Graphical User Interface with MATLAB

    Basic Matlab

    Loopsfor j=start:step:end

    .

    end;

    example:

    for j=-1:0.2:3

    .

    end;

    while Boolean expression,

    .

    end;

    example:

    while a>b,

    .

    end;

    break - Terminate execution of WHILE or FOR loop.

    break terminates the execution of FOR and WHILE loops.

    In nested loops, BREAK exits from the innermost loop only.

  • 7/30/2019 Basic Matlab GUI

    14/39

    I - 14Building a Graphical User Interface with MATLAB

    Basic Matlab

    Drawingplot(X,Y) plots vector Y versus vector Xy yellow . point - solid

    m magenta o circle : dotted

    c cyan x x-mark -. dashdot

    r red + plus -- dashedg green * star

    b blue s square

    w white d diamond

    k black v triangle (down)

    ^ triangle (up)

    < triangle (left)

    > triangle (right)

    p pentagram

    h hexagram

  • 7/30/2019 Basic Matlab GUI

    15/39

    I - 15Building a Graphical User Interface with MATLAB

    Drawing a circle

    t=0:0.1:2*pi;x=10*cos(t);

    y=10*sin(t);plot (x,y);

    Drawing a doted circle.

    t=0:0.1:2*pi;x=10*cos(t);

    y=10*sin(t);

    plot (x,y,.);

    Basic Matlab

    Drawing

  • 7/30/2019 Basic Matlab GUI

    16/39

    I - 16Building a Graphical User Interface with MATLAB

    ezplot(x^2+y^2=1);

    Basic Matlab

    Drawingezplot(x^2/5+y^2/20=1);

  • 7/30/2019 Basic Matlab GUI

    17/39

    I - 17Building a Graphical User Interface with MATLAB

    a=0:0.2:2*pi;

    b=ones(1,length(a));

    c=sin(a'*b);

    figure;

    subplot(1,2,1);

    surf(c);

    Basic Matlab

    Drawing

  • 7/30/2019 Basic Matlab GUI

    18/39

    I - 18Building a Graphical User Interface with MATLAB

    figure;

    t=0:0.1:2*pi+0.1;

    b=ones(1,length(t));z=b'*(1:1:length(t));

    x=(10*sin(z)+15).*sin(t'*b);

    y=(10*sin(z)+15).*cos(t'*b);

    surf(x,y,z);

    Basic Matlab

    Drawing

  • 7/30/2019 Basic Matlab GUI

    19/39

    I - 19Building a Graphical User Interface with MATLAB

    figure -open new figure.

    drawnow -draw to screen immediately.

    hold on -draw the next draw to the same figure and axes.

    -Transpose% - remark

    help command- return the help information on the command.

    lookfor Word - return a list of all commands that havethe desired word in their help.

    Basic Matlab

    Some useful commands

  • 7/30/2019 Basic Matlab GUI

    20/39

    I - 20Building a Graphical User Interface with MATLAB

    Basic Matlab

    Adding a path to a library

    1

    2

    3

    4

  • 7/30/2019 Basic Matlab GUI

    21/39

    I - 21Building a Graphical User Interface with MATLAB

    5

    6

    7

    8

    Adding a path to a library

  • 7/30/2019 Basic Matlab GUI

    22/39

    I - 22Building a Graphical User Interface with MATLAB

    Basic Matlab

    Adding a path to a library9

    10

  • 7/30/2019 Basic Matlab GUI

    23/39

    Building GUIs with MATLAB

    Oren Meirom

    Omikron Delta

    GUIs

  • 7/30/2019 Basic Matlab GUI

    24/39

    I - 24Building a Graphical User Interface with MATLAB

    What is a Callback?

    A callback is a sequence of commands which areimplemented by activating a graphics object:

    e.g. CreateFcn, CallBack, ButtonDownFcn, DeleteFcn.

  • 7/30/2019 Basic Matlab GUI

    25/39

    I - 26Building a Graphical User Interface with MATLAB

    The 10 styles of Matlab Uicontrol objects Push Button.

    Toggle Button.

    Check Box.

    Radio Button.

    Editable Text.

    List Box.

    Pop-up Menu.

    Slider.

    Frame.

    Static Text

  • 7/30/2019 Basic Matlab GUI

    26/39

    I - 27Building a Graphical User Interface with MATLAB

    Push/Toggle Buttons

    The push button is widely prevalent uicontrolstyle that is used primarily to indicate that adesired action should immediately take place.

    The toggle button look just like push button,

    except there is no intermediate state.Rather, thebutton will remain in its selected or not selectedstate after the user clicks on it.

    >>mcpush

  • 7/30/2019 Basic Matlab GUI

    27/39

    I - 28Building a Graphical User Interface with MATLAB

    Check Box Useful for representing two states of an option

    that you may want to provide (usually as on andoff).

    In its off state the check box will consist of anempty or unfilled square. In the on state, the

    check boxs square will contain a V sign.

    >>mccheckbox,mccheckbox1

  • 7/30/2019 Basic Matlab GUI

    28/39

    I - 29Building a Graphical User Interface with MATLAB

    Radio Button

    Similar to the check box in that there are twostates associated with each other.

    Usually two or more radio buttons are linkedtogether as a group.They are linked in the sense

    that only one of the buttons will be in its selectedstate.

    >>mcradiox

  • 7/30/2019 Basic Matlab GUI

    29/39

    I - 30Building a Graphical User Interface with MATLAB

    Editable Text Used in situations that require the user to enter

    strings or characters or numbers. The strings , inturn, are used by the application for which theinterface has been built.

    Clicking anywhere within this object will changethe mouse from a pointer to a text insertionindicator.

    >>mcedit, mceditf

  • 7/30/2019 Basic Matlab GUI

    30/39

    I - 31Building a Graphical User Interface with MATLAB

    List Boxes

    New style provided by MATLAB 5.x

    Very similar to pop-up menus.

    The main difference with a list box is that you can

    make the set of options visible to the user at alltimes.

    >>mccheckbox

    >>mclistbox

  • 7/30/2019 Basic Matlab GUI

    31/39

    I - 32Building a Graphical User Interface with MATLAB

    Pop-up Menus

    Used in situations where multiple choices need to

    be available to the user.

    When the user clicks and holds the mouse buttonanywhere within the object, a list of choices

    appear.

    >>mcpopup

  • 7/30/2019 Basic Matlab GUI

    32/39

    I - 33Building a Graphical User Interface with MATLAB

    Sliders

    Useful in representing a fixed range of valuesfrom which to choose.

    The slider has no way of explicitly indicating thenumeric value that the slider represents.

    Therefor, it is recommended that an editable textor static text style uicontrol accompany theslider.

    >>mcslider, mcslider2

  • 7/30/2019 Basic Matlab GUI

    33/39

    I - 34Building a Graphical User Interface with MATLAB

    Frames

    Provide a solid background that helps blend a set

    of uicontrols into one complete and cohesive

    interface.

    Used as an effective method of organizing the

    GUI in a logical and intuitive fashion.

  • 7/30/2019 Basic Matlab GUI

    34/39

    I - 35Building a Graphical User Interface with MATLAB

    Static TextAvailable for creating labels, status messages or

    other information pertinent to the user.

    Static text does not perform any action if the user

    clicks on any part of the object. In addition , the

    user can not edit the information that is displayed.

  • 7/30/2019 Basic Matlab GUI

    35/39

    I - 36Building a Graphical User Interface with MATLAB

    Properties that Track User Actions

    ButtonDownFcn-When clicking the mouse button while the pointer is located over or near the object.

    WindowButtonDownFcn- When clicking the mouse button down within the figure boundaries.

    WindowButtonUpFcn- When clicking the mouse button up within the figure boundaries.

    WindowButtonMotionFcn- When the mouse pointer moves within the figure boundaries. KeyboardFcn- When the figure is active.

    CreatFcn- When creating an object.

    DeleteFcn- When deleting an object.

    ResizeFcn- When resizing the figure.

  • 7/30/2019 Basic Matlab GUI

    36/39

    I - 37Building a Graphical User Interface with MATLAB

    Some useful instructions.

    AXES(axes_handle) - make the axes, current.

    Object_H=GCBO - to get the object that makethe callback.

    RBUTTON(Radio_H) - use to select and deselectradio buttons with thesame Tag Name

  • 7/30/2019 Basic Matlab GUI

    37/39

    I - 38Building a Graphical User Interface with MATLAB

    Setting figure.

    h0 = figure('Color',[0.8 0.8 0.8], ...

    'Units','Normal', ...

    'Position',[0.3 0.3 0.3 0.3], ...'Tag','Fig1');

    %Setting figure name

    set(h0,'Name','Check and radio');

  • 7/30/2019 Basic Matlab GUI

    38/39

    I - 39Building a Graphical User Interface with MATLAB

    Setting Push Button. h1 =uicon trol ('Parent',h0, 'Units', 'Norm al','Pos itio n ',[0.10.3 0.3 0.4] ,'Tag ','Pushbu tto n1');

    %Setting the callBack Function

    set(h1,'Callback','Button_1_callBack_Function');

    %Setting the string on the button

    set(h1,'String','Push.B_1');

    %Setting Tool Tip String

    set(h1,'TooltipString','Push Me To Call The CallBack Function!');

    %Setting The Text Color on the button (the foreground color)

    set(h1,'ForegroundColor',[0.0,.0,0.0]);

    %setting the background color

    set(h1,'BackgroundColor',[0.0,1.0,0.0]);

  • 7/30/2019 Basic Matlab GUI

    39/39

    I - 40Building a Graphical User Interface with MATLAB

    Edit - Box call back function.

    EditBox_H=GCBO;

    EditBoxString=get (EditBox_H,'String')