You are on page 1of 9

9/21/2013

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.

9/21/2013

Create, save, open


To create an m-file, choose New from the File menu and select m-file. This procedure brings up a text editor window in which you can enter Matlab commands. To save the m-file, simply go to the File menu and choose Save (remember to save it with the '.m' extension). To open an existing m-file, go to the File menu and choose 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.

9/21/2013

SCRIPT FILES
Scripts are the simplest kind of program file because 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-files
Script 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 >>

9/21/2013

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 is that a function accepts input from and returns output to its caller, whereas scripts do not. You define MATLAB functions in a file that begins with a line containing the function key word. You cannot define a function within a script file or at the MATLAB command line. Functions always begin with a function definition line and end either with the first matching end statement, the occurrence of another function definition line, or the end of the file, whichever comes first. Using end to mark the end of a function definition is required only when the function being defined contains one or more nested functions.

9/21/2013

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

MATLAB m-files
Function Files

The function file x2.m

>>r=3; >>d=x2(r); >>d d = 9.0 >>

>>h=x2(4.2); >>h h = 17.64 >>

9/21/2013

MATLAB m-files
Function Files

Multiple Inputs and Outputs

outputs in square brackets, [ ]

inputs in parentheses ( )

MATLAB Flow Control


example

script file to cycle through x values

function file to generate the y values

9/21/2013

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 Plotting
example y = sin(t)

the plot function alone

9/21/2013

MATLAB Plotting
example y = sin(t)

script file to generate a graph of y = sin(t)

MATLAB Plotting
example y = sin(t)

function file to generate a graph of y = sin(t) >>graphsin >>

9/21/2013

EXERCISE NO. 04

M FILE PART 1

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 y intercept as constants.

You might also like