MATLAB can execute a sequence of statements stored in a file. Such
files are called "M-files" because they must have an extension of ".m" for
its filename. Much of your work with MATLAB will be creting and refining
M-files.
There are two types of M-files:
## Script filesA script file consists of a sequence of normal MATLAB statements. If the file has the filename, say,rotate.m, then the MATLAB command
rotate will cause the statements in the file to be executed.
Variables in a script file are global and will change the value of
variables of the same name in the environment of the current MATLAB
session.
Script files are often used to enter data into a large matrix; in such a
file, entry errors can be easily edited out. If, for example, one enters
in a diskfile A = [ 1 2 3 4 5 6 7 8 ];then the MATLAB statement data will cause the assignment given in
data.m to be carried out.
An M-file can also reference other M-files, including referencing itself recursively.
## Function filesFunction files provide extensibility to MATLAB. You can create new functions specific to your problem which will then have the same status as other MATLAB functions. Variables in a function file are by default local. However, you can declare a variable to be global if you wish.We first illustrate with a simple example of a function file. function y = randint(m,n) % RANDINT Randomly generated integral matrix. % randint(m,n) returns an m-by-n such matrix with % entries between 0 and 9. y = floor(10*rand(m,n)); A more general version of this function is given as follow: function y = randint(m,n,a,b) % RANDINT Randomly generated integral matrix. % randint(m,n) returns an m-by-n such matrix with % entries between 0 and 9. % randint(m,n,a,b) returns entries between integers a and b. if nargin < 3, a=0; b=9; end y = floor((b-a+1)*rand(m,n))+a;
This should be placed in a diskfile with filename m and n in the function file with
the output result being passed out to the variable z. Since
variables in a function file are local, their names are independent of
those in the current MATLAB environment.
Note that use of A function may also have multiple output arguments. For example: function [mean, stdev] = stat(x) % STAT Mean and standard deviation % For a vector x, stat(x) returns the % mean and standard deviation of x. % For a matrix x, stat(x) returns two row % vectors containing, respectively, the % mean and standard deviation of each column. [m n] = size(x); if m == 1 m = n; % handle case of a row vector end mean = sum(x)/m; stdev = sqrt(sum(x.^2)/m -mean.^2);Once this is placed in a diskfile stat.m, a MATLAB command
[, for example. will assign the
mean and standard deviation of the entries in the vector xm, xd] = stat(x)x to
xm and xd, respectively. Single assignments can also be
made with a function having multiple output arguments. For example,
(no brackets needed around xm = stat(x)xm)
will assign the mean of x to xm.
The
This function illustrates some of the MATLAB features that can be used
to produce efficient code. Note, for example, that x, that sum is a vector function, that sqrt is a scalar function, and that the division in
sum( is a matrix-scalar operation.
x)/mThe following function, which gives the greatest common divisor of two integers via the Euclidean algorithm, illustrates the use of an error message. function y = gcd(a,b) % GCD Greatest common divisor % gcd(a,b) is the greatest common divisor % of the integers a and b, not both zero. a = round(abs(a)); b = round(abs(b)); if a == 0 & b == 0 error('The gcd is not defined when both numbers are zero') else while b ~= 0 r = rem(a,b); a = b; b = r; end end
Some more advanced features are illustrated by the following function.
As noted earlier, some of the input arguments of a function---such as
function [b, steps] = bisect(fun, x, tol) % BISECT Zero of a function of one variable via bisection method. % bisect(fun,c) returns a zero of the function. fun is a string % containing the name of a real-valued function of a single real % variable; ordinarily functions are defined in M-files. x is a % starting guess. The value returned is near a point where fun % changes sign. For example, bisect('sin',3) is pi. Note the % quote around sin. % % An optional third input argument seets a tolerance for the % relative accuracy of the result. The default is eps. An % optional second output arguments gives a matrix containing % a trace of the steps; the rows are of the form [c f(c)]. % Initialization if nargin < 3, tol = eps; end trace = (nargout == 2); if x ~= 0, dx = x/20; else, dx = 1/20; end a = x - dx; fa = feval(fun,a); b = x + dx; fb = feval(fun,b); % Find change of sign while (fa > 0) == (fb > 0) dx = 2.0*dx; a = x - dx; fa = feval(fun,a); if (fa > 0) ~= (fb > 0), break, end b = x + dx; fb = feval(fun,b); end if trace, steps = [a fa; b fb]; end % Main loop while abs(b - a) > 2.0*tol*max(abs(b),1.0) c = a + 0.5*(b - a); fc = feval(fun,c); if trace, steps = [steps; [c fc]]; end if (fb > 0) == (fc > 0) b = c; fb = fc; else a = c; fa = fc; end end
Some of MATLAB's functions are built-in while others are distributed as
M-files. The actual listing of any M-file---MATLAB's or your own---can
be viewed with the MATLAB command |