04. m-file part1

Upload: marielle-lucas-gabriel

Post on 04-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 04. M-file Part1

    1/9

    9/21/20

    M- FILES

    SCRIPT AND FUNCTION FILES

    WHAT IS M-FILE?

    An m-file, is a simple text file where you can

    place Matlab commands. When the file is run,

    Matlab reads the commands and executes them

    exactly as it would if you had typed each

    command sequentially at the Matlab prompt.

  • 8/13/2019 04. M-file Part1

    2/9

    9/21/20

    Create, save, open

    To create an m-file, choose Newfrom the Filemenuand select m-file. This procedure brings up a texteditor window in which you can enter Matlabcommands.

    To save the m-file, simply go to the Filemenu andchoose Save (remember to save it with the '.m'extension).

    To open an existing m-file, go to the File menu andchoose Open .

    Running an m-file

    After the m-file is saved with the name

    filename.m in the Matlab folder or directory, you

    can execute the commands in the m-file by

    simply typing filename at the Matlab prompt.

    If you don't want to run the whole m-file, you

    can just copy the part of m-file that you want to

    run and paste it at the Matlab prompt.

  • 8/13/2019 04. M-file Part1

    3/9

    9/21/20

    SCRIPT FILES

    Scripts are the simplest kind of program filebecause they have no input or output

    arguments. They are useful for automating series

    of MATLAB commands, such as computations

    that you have to perform repeatedly from the

    command line.

    MATLAB m-filesScript Files

    On the command line

    >>x=3.0;

    >>y=x^2;

    >>y

    y =

    9.0

    >>

    In the script file named test.m

    On the command line

    >>test

    y =

    9.0

    >>

  • 8/13/2019 04. M-file Part1

    4/9

    9/21/20

    MATLAB m-files

    Script Files

    script files share the workspace memory

    >>x=5.0;

    >>test

    >>y

    y =

    25.0

    >>

    test.m script

    Function files

    The main difference between a script and a function isthat a function accepts input from and returns outputto its caller, whereas scripts do not. You defineMATLAB functions in a file that begins with a linecontaining the function key word. You cannot define afunction within a script file or at the MATLABcommand line.

    Functions always begin with a function definition lineand end either with the first matching end statement,the occurrence of another function definition line, orthe end of the file, whichever comes first. Using end tomark the end of a function definition is required onlywhen the function being defined contains one or morenested functions.

  • 8/13/2019 04. M-file Part1

    5/9

    9/21/20

    MATLAB m-files

    Function Files

    Matlab identifies function files from script files by using the

    function and return keywords

    the name of the function file must be the same

    name as the function

    The function file x2.m

    MATLAB m-filesFunction Files

    >>r=3;

    >>d=x2(r);

    >>d

    d =

    9.0

    >>

    >>h=x2(4.2);

    >>h

    h =

    17.64

    >>

  • 8/13/2019 04. M-file Part1

    6/9

    9/21/20

    MATLAB m-files

    Function Files

    Multiple Inputs and Outputs

    outputs in square brackets, [ ] inputs in parentheses ( )

    MATLAB Flow Controlexample

    script file to cycle through x values

    function file to generate the y values

  • 8/13/2019 04. M-file Part1

    7/9

    9/21/20

    MATLAB Plotting

    Basic 2D plotting functions

    plot(x1,y1[,x2,y2,x3,y3.....])

    xlabel(x axis name)

    ylabel(y axis name)

    title(graph name)

    Additional functions

    grid on

    grid off

    axis([xmin,xmax,ymin,ymax])

    MATLAB Plottingexample y = sin(t)

    the plot function alone

  • 8/13/2019 04. M-file Part1

    8/9

    9/21/20

    MATLAB Plottingexample y = sin(t)

    script file to generate agraph of y = sin(t)

    function file to generate a

    graph of y = sin(t)

    >>graphsin

    >>

    MATLAB Plottingexample y = sin(t)

  • 8/13/2019 04. M-file Part1

    9/9

    9/21/20

    M

    FILE PART 1

    EXERCISE NO. 04

    M-file part1

    Use m-file to compute the ff functions

    x = t sin(t)

    y = (t-1) / (t+1)

    z = sin(t^2) / (t^2)

    Create an m-file that will compute for the slope

    of a straight line having your x and yintercept

    as constants.