Next: Returning From a Function, Previous: Variable-length Argument Lists, Up: Functions and Scripts
It is possible to return a variable number of output arguments from a
function using a syntax that's similar to the one used with the
varargin
keyword. To let a function return a variable number of
output arguments the varargout
keyword is used. As with
varargin
varargout
is a cell array that will contain the
requested output arguments.
As an example the following function sets the first output argument to 1, the second to 2, and so on.
function varargout = one_to_n () for i = 1:nargout varargout{i} = i; endfor endfunction
When called this function returns values like this
[a, b, c] = one_to_n () a = 1 b = 2 c = 3
Copy the input parameters into the corresponding output parameters. If only one input parameter is supplied, its value is copied to each of the outputs.
For example,
[a, b, c] = deal (x, y, z);is equivalent to
a = x; b = y; c = z;and
[a, b, c] = deal (x);is equivalent to
a = b = c = x;