| Contents | Index |
function [out1, out2, ...] = myfun(in1,
in2, ...)
function [out1, out2, ...] = myfun(in1, in2, ...) declares the function myfun, and its inputs and outputs. The function declaration must be the first executable line of any MATLAB function.
The existing commands and functions that compose the new function reside in a text file that has a .m extension to its filename.
MATLAB program files can be either scripts or functions. Scripts are simply files containing a sequence of MATLAB statements. Functions make use of their own local variables and accept input arguments.
The name of a MATLAB function file begins with an alphabetic character and has a filename extension of .m. The file name, less its extension, is what MATLAB searches for when you try to use the script or function.
The first line of a function defines the syntax with which you call it. The name you give to a function, as defined in the first line, should be the same as the name of the file containing the function, but without the .m extension.
The variables within the body of the function are all local variables.
A subfunction,visible only to the other functions in the same file, is created by defining a new function with the function keyword after the body of the preceding function or subfunction. Subfunctions are not visible outside the file where they are defined.
You can terminate any type of function with an end statement, but doing so is not required unless the file containing the function also contains one or more nested functions. Within this file, every function (including primary, nested, private, and subfunctions) must be terminated with an end.
Functions normally return when the end of the function is reached. Use a return statement to force an early return.
When MATLAB does not recognize a function by name, it searches for a file of the same name on disk. If the function is found, MATLAB compiles it into memory for subsequent use. The section Determining Which Function Gets Called in the MATLAB Programming Fundamentals documentation explains how MATLAB interprets variable and function names that you enter, and also covers the precedence used in function dispatching.
When you call a function from the command line or from another function, MATLAB parses the function and stores it in memory. The parsed function remains in memory until cleared with the clear command or you quit MATLAB. The pcode command performs the parsing step and stores the result on the disk as a P-file to be loaded later.
The existence of a file on disk called stat.m containing this code defines a new function called stat that calculates the mean and standard deviation of a vector:
function [mean,stdev] = stat(x) n = length(x); mean = sum(x)/n; stdev = sqrt(sum((x-mean).^2/n));
Call the function, supplying two output variables on the left side of the equation:
[mean stdev] = stat([12.7 45.4 98.9 26.6 53/1]) mean = 47.3200 stdev = 29.4085
avg is a subfunction within the file stat.m:
function [mean,stdev] = stat2(x) n = length(x); mean = avg(x,n); stdev = sqrt(sum((x-avg(x,n)).^2)/n); function mean = avg(x,n) mean = sum(x)/n;
Call the function and compare the answer with that of Example 1, above:
[mean stdev] = stat2([12.7 45.4 98.9 26.6 53/1]) mean = 47.3200 stdev = 29.4085
nargin | nargout | pcode | varargin | varargout | what

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |