Top Banner

of 40

Basic Matlab and Gui

Apr 05, 2018

Download

Documents

abclatech
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
  • 8/2/2019 Basic Matlab and Gui

    1/40

    I - 1Building a Graphical User Interface with MATLAB

    Basic MatlabMatrix definition

    a=[1,2 ; 3,4] => a= 1 23 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

  • 8/2/2019 Basic Matlab and Gui

    2/40

    I - 2Building a Graphical User Interface with MATLAB

    Basic MatlabExample

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

    b= 1 2 3 4 51 2 3 4 5

    1 2 3 4 5

    Example

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

    b= 1 1 1 12 2 2 2

    3 3 3 3

    4 4 4 45 5 5 5Example (transpose)

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

    b=ab= 1 2 3 4 5

  • 8/2/2019 Basic Matlab and Gui

    3/40

    I - 3Building a Graphical User Interface with MATLAB

    Basic Matlab

    Strings

    a=[a,b,c]a= abc

    b=[a;a;a]

    b= abc

    abcabc

    Strings

    a=[a,b,c]a= abc

    b=[a,a,a]

    b= abcabcabc

    Strings

    a=[a,b,c]b=[a,c,c]

    c=(a==b);

    c= 1 0 1

  • 8/2/2019 Basic Matlab and Gui

    4/40

    I - 4Building a Graphical User Interface with MATLAB

    Basic Matlab

    1 2

    3 4

    5 6

    7 8

    6 8

    10 12

    19 22

    * 43 50

    A

    B

    A B

    A B

    ====

    ====

    + =+ =+ =+ =

    ====

    5 12

    .* 21 32

    1 4

    .^ 2 9 16

    sin(1) sin(2)

    sin( ) sin(3) sin(4)

    A B

    A

    A

    ====

    ====

    ====

  • 8/2/2019 Basic Matlab and Gui

    5/40

    I - 5Building a Graphical User Interface with MATLAB

    Basic Matlab

    A=zeros(a1

    , a2

    , a3

    , an

    );A is an ndimensional matrix of zeros.A=ones(a1, a2, a3, an);

    A is ann

    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.

  • 8/2/2019 Basic Matlab and Gui

    6/40

    I - 6Building a Graphical User Interface with MATLAB

    Basic Matlab

    A(m,n) returns the value of the matrix in row-mand 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.

  • 8/2/2019 Basic Matlab and Gui

    7/40

    I - 7Building a Graphical User Interface with MATLAB

    Basic MatlabFunctions in Matlab

    function [output variables]=function_name (input variables)

    Input and output variables, can be of any type.

  • 8/2/2019 Basic Matlab and Gui

    8/40

    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 MatlabFunctions in Matlab

  • 8/2/2019 Basic Matlab and Gui

    9/40

    I - 9Building a Graphical User Interface with MATLAB

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

    output:

    a = 10b = helloworld

    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 12b = hello

    world

    c = 1 2 3 4 5

    Basic MatlabFunctions in Matlab

  • 8/2/2019 Basic Matlab and Gui

    10/40

    I - 10Building a Graphical User Interface with MATLAB

    Basic MatlabBit-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.

  • 8/2/2019 Basic Matlab and Gui

    11/40

    I - 11Building a Graphical User Interface with MATLAB

    Basic Matlabconditions

    If ( Boolean expression).end;

    Boolean expression== : Is Equal~=: Not Equal

    > : Is grater then< : Is less Then>=: Is grater then or equal to

  • 8/2/2019 Basic Matlab and Gui

    12/40

    I - 12Building a Graphical User Interface with MATLAB

    Basic Matlabconditions

    switch switch_expr

    case case_expr,statement, ..., statement

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

    ...

    otherwise,statement, ..., statement

    end

  • 8/2/2019 Basic Matlab and Gui

    13/40

    I - 13Building a Graphical User Interface with MATLAB

    Basic MatlabLoops

    for 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.

  • 8/2/2019 Basic Matlab and Gui

    14/40

    I - 14Building a Graphical User Interface with MATLAB

    Basic MatlabDrawingplot(X,Y) plots vector Y versus vector Xy yellow . point - solidm magenta o circle : dotted

    c cyan x x-mark -. dashdot

    r red + plus -- dashed

    g green * starb blue s square

    w white d diamond

    k black v triangle (down)

    ^ triangle (up)

    < triangle (left)

    > triangle (right)

    p pentagramh hexagram

  • 8/2/2019 Basic Matlab and Gui

    15/40

    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 MatlabDrawing

  • 8/2/2019 Basic Matlab and Gui

    16/40

    I - 16Building a Graphical User Interface with MATLAB

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

    Basic MatlabDrawing

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

  • 8/2/2019 Basic Matlab and Gui

    17/40

    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 MatlabDrawing

  • 8/2/2019 Basic Matlab and Gui

    18/40

    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 MatlabDrawing

  • 8/2/2019 Basic Matlab and Gui

    19/40

    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 MatlabSome useful commands

  • 8/2/2019 Basic Matlab and Gui

    20/40

    I - 20Building a Graphical User Interface with MATLAB

    Basic MatlabAdding a path to a library

    1

    2

    3

    4

  • 8/2/2019 Basic Matlab and Gui

    21/40

    I - 21Building a Graphical User Interface with MATLAB

    5

    6

    7

    8

    Adding a path to a library

  • 8/2/2019 Basic Matlab and Gui

    22/40

    I - 22Building a Graphical User Interface with MATLAB

    Basic MatlabAdding a path to a library9

    10

  • 8/2/2019 Basic Matlab and Gui

    23/40

    Building GUIs with MATLAB

    Oren Meirom

    Omikron Delta

    GUIs

    B ildi G hi l U I f i h M

  • 8/2/2019 Basic Matlab and Gui

    24/40

    I - 24Building a Graphical User Interface with MATLAB

    What is a Callback? A callback is a sequence of commands which are

    implemented by activating a graphics object:

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

    B ildi G hi l U I t f ith MATLAB

  • 8/2/2019 Basic Matlab and Gui

    25/40

    I - 25Building a Graphical User Interface with MATLAB

    Storing information in Tag! This property can store a string vector, which

    means that you can assign a unique name to

    every single graphics object.

    ! Provides an alternative to the technique ofstoring the variables in the userdata property.

    B ildi G hi l U I t f ith MATLAB

  • 8/2/2019 Basic Matlab and Gui

    26/40

    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

    IBuilding a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    27/40

    I - 27Building a Graphical User Interface with MATLAB

    Push/Toggle Buttons! The push button is widely prevalent uicontrol

    style that is used primarily to indicate that a

    desired action should immediately take place.

    ! The toggle button look just like push button,except there is no intermediate state.Rather, the

    button will remain in its selected or not selectedstate after the user clicks on it.

    >>mcpush

    I 28Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    28/40

    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 and

    off).! In its off state the check box will consist of an

    empty or unfilled square. In the on state, thecheck boxs square will contain a V sign.

    >>mccheckbox,mccheckbox1

    I 29Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    29/40

    I - 29Building a Graphical User Interface with MATLAB

    Radio Button! Similar to the check box in that there are two

    states associated with each other.

    ! Usually two or more radio buttons are linkedtogether as a group.They are linked in the sensethat only one of the buttons will be in its selected

    state.

    >>mcradiox

    I 30Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    30/40

    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 , in

    turn, are used by the application for which theinterface has been built.

    ! Clicking anywhere within this object will change

    the mouse from a pointer to a text insertionindicator.

    >>mcedit, mceditf

    I 31Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    31/40

    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

    I 32Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    32/40

    I - 32Building a Graphical User Interface with MATLAB

    Pop-up Menus

    !

    Used in situations where multiple choices need tobe available to the user.

    ! When the user clicks and holds the mouse buttonanywhere within the object, a list of choicesappear.

    >>mcpopup

    I - 33Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    33/40

    I - 33Building a Graphical User Interface with MATLAB

    Sliders! Useful in representing a fixed range of values

    from which to choose.

    ! The slider has no way of explicitly indicating thenumeric value that the slider represents.Therefor, it is recommended that an editable text

    or static text style uicontrol accompany theslider.

    >>mcslider, mcslider2

    I - 34Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    34/40

    I 34g p

    Frames! Provide a solid background that helps blend a set

    of uicontrols into one complete and cohesiveinterface.

    ! Used as an effective method of organizing theGUI in a logical and intuitive fashion.

    I - 35Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    35/40

    I 35g p

    Static Text! Available for creating labels, status messages or

    other information pertinent to the user.

    ! Static text does not perform any action if the userclicks on any part of the object. In addition , theuser can not edit the information that is displayed.

    I - 36Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    36/40

    I 36g p

    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.

    I - 37Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    37/40

    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 the

    same Tag Name

    I - 38Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    38/40

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

    I - 39Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    39/40

    Setting Push Button.! h1 =uicontrol ('Parent',h0, 'Units','Normal','Position',[0.1

    0.3 0.3 0.4] ,'Tag','Pushbutton1');

    ! %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]);

    I - 40Building a Graphical User Interface with MATLAB

  • 8/2/2019 Basic Matlab and Gui

    40/40

    Edit - Box call back function.

    ! EditBox_H=GCBO;

    ! EditBoxString=get (EditBox_H,'String')