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 files and function 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 data.m 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 randint.m (corresponding to the function name). The first line declares the function name, input arguments and output arguments; without this line the file would be a script file. Then a MATLAB statement z = randint(4,5), for example, will cause the numbers 4 and 5 to be passed to the variables 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:
The % symbol indicates that the rest of the line is a comment;
MATLAB will ignore the rest of the line. However, the first few comments
lines, which document the M-file, are available to the on-line help
facility and will be displayed if, for example, help stat is
entered. Such documentation should always be included in a
function file.
This function illustrates some of the MATLAB features that can be used
to produce efficient code. Note, for example, that x.^2 is
the matrix squares of the entries of x, that sum is a vector function, that sqrt is a scalar function, and that the division in
sum(x)/m is a matrix-scalar operation.
The following function, which gives the greatest common divisor of two
integers via the Euclidean algorithm, illustrates the use of an error message.
Some more advanced features are illustrated by the following function.
As noted earlier, some of the input arguments of a function---such as
tol in the following example, may b made optional through use of
nargin ("number of input arguments"). The variable nargout
can be similarly used. Note that the fact that a relation is a number (1
when true; 0 when false) is used and that, when while of
if evalutes a relation, "nonzero" means "true" and "zero" means
"false". Finally, the MATLAB function feval permits one to have
as an input variable a string naming function.
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 type functionname. Try
entering type eig, type vander and type rank.
|