Write a function in MATLAB and call the Function Method in the main program.
It is the same as other programming languages,Sub-functions and primary functions must be stored in different files.,The file name is the function name..Files must be saved in current directory before calling.
Basic Function Structure:
Function [return Variable list] = function name (input variable List)
% Annotation statement
Detection of input and return variable formats
Function body
% -------- Example -----
Main function main. m
Y = test (X)
Sub-function test. m (the file name is the function name)
Function Z = test (X)
Z = x. * X;
% ------- Multi-dimensional output --------
[A, B, c] = AA (m, n)
In addition, there are recursive calls, that is, the function itself can be called within the function;
Variable input/output count processing.
Function A = convs (varargin)
A = 1;
For I = 1: length (varargin)
A = Conv (A, varargin [I]);
End
Compiling and calling of MATLAB Functions