A Brief Explanation of MATLAB M-Files

MATLAB can execute a sequence of statements stored on diskfiles. Such files are called "M-files" because they must have the file type of ".m" as the last part of their filename.

Much of your work with MATLAB will be in creating and refining M-files.

Script Files and Function Files

There are two types of M-files: script files and function files.

Script Files

A script file consists of a sequence of normal MATLAB statements. If the file has the filename, say, rotate.m, then the MATLAB command


will cause the statements in the file to be executed. Variables in a script file are global and will change the value of the environment. 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 the following in a diskfile, data.m:

       A = [
       1 2 3 4
       5 6 7 8

then the MATLAB statement


will cause the assignment given in data.m to be carried out.

An M-file can reference other M-files, including referencing itself recursively.

Function Files

Function 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 local.

We first illustrate with a simple example of a function file:

     function P = prodsqr(A,B)
       PRODSQR Product of the square of two matrices.
      P = A^2*B^2;

Memory will be used more efficiently if A is overwritten with the result:

     function A = prodsqr(A,B)
       PRODSQR Product of the square of two matrices.
      A = A^2*B^2;

This should be placed in a diskfile with filename prodsqr.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 = prodsqr(x,y)

for example, will cause the variables x and y to be passed to the variables A and B in the function file, with the output result being passed to the variable z. Since variables in a function file are local, their names are independent of those in the environment.

A function may also have multiple output argurnents. 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 rov vector
      mean = sum(x)/m;
      stdev = sqrt(sum(x.^2)/m - mean.^2)

Once this is placed in a diskfile stat.m, the MATLAB command

        [xm, xd] = stat(x)

for example, will assign the mean and standard deviation of the entries in the vector x to xm and xd, respectively. Single assignments can also be made with a function having multiple output arguments. For example,

        xm = stat(x)

will assign the mean of x to xm.

This function illustrates some of the MATLAB features which you can use use to produce efficient code. Note, for example, that x.^2 is the matrix of squares of the entries of x, that sum is a vector function (see the section on "Matlab Vector Functions"), that sqrt is a scalar function (see the section on "Matlab Scalar Functions"), and that the division in sum(x)/m is a matrix-scalar operation.

The percent sign () symbol indicates that the text following the percent sign is a comment; MATLAB will ignore the rest of the line. However, the first few comment lines, which document the M-file, are available to the online help facility and will be displayed if, for example, you enter the command:

        help stat

Other Matlab Hints

Click on an item listed below to access additional MATLAB-related hints.

Return to ACS home page.

Please send comments and suggestions to