More on Functions
In general, all functions have the form:
function [output1, output2, ...] = myFunctionName(input1, input2, ...)
where output1 refers to the first output, input1 refers to the first input etc.
Any comment lines immediately below the function definition till the first command in the body of the function appear when the help myFunctionName command is used. Try it with the function we created (help maxVal).
nargin & nargout
MATLAB is quite flexible when it comes to specifying inputs and outputs. The function nargin returns the number of input arguments (nargin = number of arguments in) supplied and nargout returns the number of outputs requested (nargout = number of arguments out).
Inside the body of functions nargin and nargout return the number of input arguments that were used to call the function. As an illustration, say you write a function that iterates until a convergence criteria is met or a maximum number of iterations has been reached. One way to write such a function is to make the convergence criteria and the maximum number of iterations be optional arguments:
function x = SolveIt(a,tol,MaxIters)
if ( lt(nargin, 3) )
MaxIters = 100;
end
if ( lt(nargin, 2) )
tol = sqrt(eps);
end
...
NB: the reason I have had to use 'lt' (syntax for 'less than') instead of the symbol '<', is because of blogger. '<' when used within parentheses, seems to ignore any number after '<'. In practice there is no difference. You may use either.
The command 'nargout' works similarly. An example would be:
function [x_sort,x_mean,x_med,x_std] = myMarks(x)
x_sort = sort(x);
if nargout > 1, x_mean = mean(x); end
if nargout > 2, x_med = median(x); end
if nargout > 3, x_std = std(x); end
Outside the body of the function (or in command window), the functions nargin and nargout work differently. nargin('fun') and nargout('fun') respectively return the number of declared inputs and outputs for the function 'fun'. For example, in the example discussed previously nargin('maxVal') returns 1 and nargout('maxVal') returns 3.
varargin & varargout
To specify a variable (not fixed in advance) number of inputs to and outputs from a function argument one can use varargin (variable arguments in) and varargout (variable arguments out). To illustrate, here is an example taken from the Mathworks site:
function vartest(argA, argB, varargin)
optargin = size(varargin,2);
stdargin = nargin - optargin;
fprintf('Number of inputs = %d\n', nargin)
fprintf('Inputs from individual arguments(%d):\n', stdargin)
if stdargin >= 1
fprintf('%d\n', argA)
end
if stdargin == 2
fprintf('%d\n', argB)
end
fprintf('Inputs packaged in varargin(%d):\n', optargin)
for k = 1 : size(varargin, 2)
fprintf('%d\n', varargin{k})
end
A few points about varargin (& varargout):
The varargin (varargout) statement is used only inside a function M-file to contain optional input (output) arguments passed to the function. The varargin (varargout) argument must be declared as the last input (output) argument to a function, collecting all the inputs (outputs) from that point onwards. In the declaration, varargin (varargout) must be lowercase.
function [s, varargout] = mysize(x)
nout = max(nargout, 1) - 1;
s = size(x);
for k = 1 : nout, varargout(k) = {s(k)}; end
returns the size vector and, optionally, individual sizes. So
>> [s, rows, cols] = mysize(rand(4,5));
returns s = [4 5], rows = 4, cols = 5
And finally, though I may have mentioned it earlier, name of the M-file takes precedence over the name used in the function definition. That is, a user-defined function is called by the name of the M-file, and not the name given in the function definition. In general, it is always advisable to keep them the same.