matlab help_ m files

Upload: giaannguyen

Post on 01-Mar-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Matlab Help_ m Files

    1/11

    MATLAB HELP

    How do I Create a new MATLAB file ?

    Since an m-file is nothing more than a text file it canbe created

    using any text editor however, MATLAB rovides its own editor

    that rovides some articular features that are useful when

    writing!editing functions"

    To oen a new m-file# $n the MATLAB command window, go to

    %$L& on the toolbar, select '&(, then select M-%$L&" This oens

    the MATLAB editor!debugger and gives an emty file in which

    you can create whatever m-file you want"

    How do I Save a MATLAB file?

    )nce you have finished writing your cod!rogram you have to save

    it as an m-file before you can use it" This is done in the same way

    you save a file in any other alication#

    go to %$L&, and SA*&"

    tye in the name that you want to use+""it is best to always use a

    descritor see your directions for turning in roects. as the /file

    name0 ....you don1t need to exlicitly secify the file tye as 2"m

    navigate to the folder where you want to save the function file ....

    see below for more details on /(here to Save an M-%ile30

    clic4 on SA*& Where to Save an M-File? $t doesn1t really matterwhere you store it""" B5T when you want to use it, it needs to be

    somewhere in /MATLAB1s ath0or should be in MATLAB1s

    resent wor4ing directory 6(7.

    the ath secifies all the folders where MATLAB will loo4 for a m

  • 7/26/2019 Matlab Help_ m Files

    2/11

    file when your the rogram!code is run

    the 6(7 secifies a single folder that MATLAB considers its

    rimary folder for storing things it is generally advisable to

    secify an aroriate 6(7 each time you start u MATLAB andsecify it to be wherever you have the m-files you are wor4ing on

    8lic4 on %$L&, clic4 on S&T 6AT9, clic4 on B:)(S&,

    navigate to the folder you want as 6(7 and clic4 on it, and then

    clic4 );

    How do I Run an M-File?

    )nce you have a rogram!code saved as an m-file with a name and

    in a folder that is either the 6(7 or is in MATLAB1s ath, you can

    run it from the command line#

    Functions

    What is a MATLAB function?

    A MATLAB /function0 is a MATLAB rogram that erforms a

    se

  • 7/26/2019 Matlab Help_ m Files

    3/11

    variables as oututs and may also generate lots, etc" sometimes a

    function doesn1t return any outut variables but instead ust

    generates lots, etc"."

    How do I Create a new MATLAB function?

    Since an m-file is nothing more than a text file it canbe created

    using any text editor however, MATLAB rovides its own editor

    that rovides some articular features that are useful when

    writing!editing functions"

    To oen a new m-file# $n the MATLAB command window, go to

    %$L& on the toolbar, select '&(, then select M-%$L&" This oens

    the MATLAB editor!debugger and gives an emty file in whichyou can create whatever m-file you want"

    What do I have to put on the First Line of a MATLAB

    function?

    The =st line of a functionmust contain the /function definition,0

    which has a general structure li4e this see also the secific

    examle below.

    =

    #function [Out_1,Out_2,...,Out_N] =function_name(In_1,In_2,...,In_M)whereOut_1,Out_2,...,Out_N are the ' outut variables andIn_1,In_2,...,In_M are the

    M inut variables>$f there is only a single outut variable use#

    function Out_1 = function_name(In_1,In_2,...,In_M) $f there is

    no outut variable use#functionfunction_name(In_1,In_2,...,In_M)

    What do I have to put after the 1st

    line?

    After the first line, you ust ut a se

  • 7/26/2019 Matlab Help_ m Files

    4/11

    comuting in MATLAB in the command line environment" This

    se

  • 7/26/2019 Matlab Help_ m Files

    5/11

    variable name."

    How do I Save a MATLAB function?

    )nce you have finished writing your function you have to save itas an m-file before you can use it" This is done in the same way

    you save a file in any other alication#

    go to %$L&, and SA*&"

    tye in the name that you want to use o it is best to always use the

    /function name0 as the /file name0 o you don1t need to exlicitly

    secify the file tye as 2"m

    navigate to the folder where you want to save the function file o

    see below for more details on /(here to Save an M-%ile30

    clic4 on SA*& Where to Save an M-File? $t doesn1t really matter

    where you store it""" B5T when you want to use it, it needs to be

    somewhere in /MATLAB1s ath0or should be in MATLAB1s

    resent wor4ing directory 6(7.

    the ath secifies all the folders where MATLAB will loo4 for a

    function1s file when the function is run

    the 6(7 secifies a single folder that MATLAB considers its

    rimary folder for storing things it is generally advisable to

    secify an aroriate 6(7 each time you start u MATLAB and

    secify it to be wherever you have the m-files you are wor4ing on

    o 8lic4 on %$L&, clic4 on S&T 6AT9, clic4 on B:)(S&,navigate to the folder you want as 6(7 and clic4 on it, and then

    clic4 );

    @

  • 7/26/2019 Matlab Help_ m Files

    6/11

    How do I Run an M-File?

    )nce you have a function saved as an m-file with a name the same

    as the function name and in a folder that is either the 6(7 or is in

    MATLAB1s ath, you can run it from the command line#

    "" [Out_1,Out_2,...,Out_N] =function_name(In_1,In_2,...,In_M)

    How do I Test an M-File?

    )nce you have a file written you need to test it" (hen you try to

    run it the first time there is a good chance that it will have some

    syntax error that will cause its oeration to terminate MATLAB

    will tell you on what line the oeration was stoed, so you can

    focus immediately on somewhere further along in the function and

    will give you some idea if what the roblem is" ust 4ee at it and

    you1ll eventually get it to run, but here is a ti#

    oen the m-file that you are debugging

    clic4 on 7&B5C on the toolbar at to

    clic4 on ST)6 $% &::): o note# there some other otions that

    can be useful, so try them out

    'ow when you run your function and it encounters an error, it will

    sto and will ut you into a mode that will allow you to view allthe variables at the oint at which the error occurred o (hen you

    are stoed i"e", /in debug mode0. you have a different romt

    than MATLAB1s standard DD romt o To

  • 7/26/2019 Matlab Help_ m Files

    7/11

    (hen you are all done using this feature you can turn it off by

    clic4ing on 7&B5C on the toolbar and then clic4ing on ST)6 $%

    &::): which should be chec4ed to indicate that it was turned

    on. o Then you are returned to having the standard romt Then,

    once you have it running without any syntax errors or warnings,

    you need to test it to verify that it really does what you intended it

    to do" )bviously, for simle functions you may be able to verify

    that it wor4s by running a few examles and chec4ing that the

    oututs are what you exect" 5sually you need to do

  • 7/26/2019 Matlab Help_ m Files

    8/11

    Hou can even modify a variable1s contents to correct a roblem."

    'ote that the )'LH variable you have access to are those that have

    been created inside the function or have been assed to it via the

    inut arguments" 'ote that you can set multile brea4oints at a

    time once you have stoed at the first one you can clic4 on7&B5C and then clic4 on 8)'T$'5& and it will ic4 u

    execution immediately where it left off but with the modified

    variables if you changed anything." 'ote also that once you have a

    function stoed in debug mode you can /single-ste0 through the

    next several lines# clic4 on 7&B5C and clic4 on S$'CL& ST&6"

    Comments on Programming Style

    =" $n many ways, rogramming in MATLAB is a lot li4erogramming in 8, but there are some significant differences" Most

    notably, MATLAB can oerate directly on vectors and matrices

    whereas in 8 you must oerate directly on individual elements of

    an array" Because of this, loos are M589 less common in

    MATLAB than they are in 8# in 8, if you want to add two vectors

    you have to loo over the elements in the vectors, adding an

    element to an element in each iteration of the loo> in MATLAB,

    you ust issue a command to add the two vectors together and thevector addition of all the elements is done with this single

    command"

    @" The /comment symbol0 in MATLAB is I" Anything that occurs

    after a I on a line is considered to be comments"

    F" $t is often helful to ut several comment lines right after the

    function definition line" These comments exlain what the function

    does, what the inuts and oututs are, and how to call the function"

    J" 6utting in lots of comments hels you and others understand what

    the function does from a grading oint of view you will have a

    higher robability of getting full credit if you write comments that

    tell me what your code is doing

  • 7/26/2019 Matlab Help_ m Files

    9/11

    An Example

    9ere is an examle of a MATLAB function" Suose you would

    li4e to write a general routine that will comute the value of some

    arbitrary olynomial at an arbitrary set of e

  • 7/26/2019 Matlab Help_ m Files

    10/11

    % First, generate the x values as a row vectorx =

    xmin:del_x:xmax; % this uses MATLAB's colon operator to

    generate

    % equally spaced points at a

    spacing of del_xfor n=0:power% Loop through the powers in the

    polynomial% and compute a row vector for each of the

    terms in the polynomial% and put that row vectro into

    each row of matrix X X(n+1,:)=coeffs(n+1)*x.^n; % note

    use of "n+1" for indexing - matlab

    can't index using "0"

    end

    % You now have a matrix X who's nth row is C_n*x.^n%The x values run across this matrix, the power value

    runs down this matrix

    % Now to create the values of y we have to add the rows

    together:

    if power>0y=sum(X); % when matlab's sum function is

    applied to a matrix it sums

    down the

    % columns to give a row vectory=X; % if power=0 there

    is only a single row in X, so no need to sum

    'ow, to run this function we could do this#

    K y,xN O olye K y@,x@N O olye

  • 7/26/2019 Matlab Help_ m Files

    11/11

    valuesQ. K ylabelQy valuesQ. K sublotF,=,F.

    K lotx@,[email protected] xlabelQx valuesQ. K ylabelQy valuesQ.

    'ote that we ran the function three times each time we ut in

    different values for the inut variables and called the outut

    variables something different each time so that we could store the

    three different results" (e then lotted the results on three different

    sublots and labeled the axes"

    AL(AHS LAB&L AR&S AL(AHS LAB&L AR&S

    AL(AHS LAB&L AR&S AL(AHS LAB&L AR&S

    And label the 5'$TS of the values if it ma4es sense