Programming |

The first function in any M-file is called the *primary function*. Following the primary function can be any number of subfunctions, which can serve as subroutines to the primary function.

Under most circumstances, the primary function is the only function in an M-file that you can call from the MATLAB command line or from another M-file function. You invoke this function using the name of the M-file in which it is defined.

For example, the `average`

function shown here resides in the file `average.m`

:

function y = average(x) % AVERAGE Mean of vector elements. y = sum(x)/length(x); % Actual computation

You can invoke this function from the MATLAB command line with this command to find the average of three numbers:

Note that it is customary to give the primary function the same name as the M-file in which it resides. If the function name differs from the filename, then you must use the filename to invoke the function.

Examples of Anonymous Functions | Nested Functions |

© 1994-2005 The MathWorks, Inc.