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 special varargin
parameter name. To let a function return a variable number of output arguments the special output parameter name varargout
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
If varargin
(varargout
) does not appear as the last element of the input (output) parameter list, then it is not special, and is handled the same as any other parameter name.
Copy the input parameters into the corresponding output parameters.
If only a single 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;
Programming Note: deal
is often used with comma separated lists derived from cell arrays or structures. This is unnecessary as the interpreter can perform the same action without the overhead of a function call. For example:
c = {[1 2], "Three", 4}; [x, y, z] = c{:} ⇒ x = 1 2 y = Three z = 4
See also: cell2struct, struct2cell, repmat.
© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Variable_002dlength-Return-Lists.html