Professional Documents
Culture Documents
M- 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
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
9/21/2013
MATLAB m-files
Script Files
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
9/21/2013
MATLAB m-files
Function Files
inputs in parentheses ( )
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)
MATLAB Plotting
example y = sin(t)
9/21/2013
MATLAB Plotting
example y = sin(t)
MATLAB Plotting
example y = sin(t)
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.