This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Variable-length output argument list





varargout is an output variable in a function definition statement that enables the function to return any number of output arguments. Specify varargout using lowercase characters, and include it as the last output argument after any explicitly declared outputs.

When the function executes, varargout is a 1-by-N cell array, where N is the number of outputs requested after the explicitly declared outputs.


collapse all

Define a function in a file named returnVariableNumOutputs.m that returns an output size vector s and a variable number of additional outputs.

type returnVariableNumOutputs
function [s,varargout] = returnVariableNumOutputs(x)
    nout = max(nargout,1) - 1;
    s = size(x);
    for k = 1:nout
        varargout{k} = s(k);

Output s contains the dimensions of the input array x. Additional outputs correspond to the individual dimensions within s.

Call the function with a three-dimensional array and request three outputs.

A = rand(4,5,2);
[s,rows,cols] = returnVariableNumOutputs(A)
s = 

     4     5     2

rows = 4
cols = 5

Call the function again with a four-dimensional array and request four outputs. This time, the function does not return the individual fourth dimension.

A = zeros(1,4,5,2);
[s,dim1,dim2,dim3] = returnVariableNumOutputs(A)
s = 

     1     4     5     2

dim1 = 1
dim2 = 4
dim3 = 5

Call the function once more on A and request one output. Now the function returns the dimensions of A and not varargout.

s = returnVariableNumOutputs(A)
s = 

     1     4     5     2

Define a function in a file named variableNumInputAndOutput.m that accepts a variable number of inputs and outputs.

type variableNumInputAndOutput
function varargout = variableNumInputAndOutput(varargin)
    disp("Number of provided inputs: " + length(varargin))
    disp("Number of requested outputs: " + nargout)
    for k = 1:nargout
        varargout{k} = k;

Call the function with two inputs and three outputs.

[d g p] = variableNumInputAndOutput(6,'Nexus')
Number of provided inputs: 2
Number of requested outputs: 3
d = 1
g = 2
p = 3

Call the function again with no inputs or outputs.

Number of provided inputs: 0
Number of requested outputs: 0

In a file in your working folder, create a wrapper to the plot function that plots a red line. The redplot function accepts a variable-length input argument list and returns a variable-length output argument list. It sets the line color to red, and forwards other input values to the plot function. This function wrapper allows you to pass redplot the same inputs as plot and not specify that the line color is red.

type redplot.m
function varargout = redplot(varargin)
    [varargout{1:nargout}] = plot(varargin{:},'Color',[1,0,0]);

Use redplot to create a line plot.

x = 0:pi/100:2*pi;
y = sin(x);

Call redplot again, and specify input and output arguments to forward to the plot function.

h = redplot(x,y,'Marker','o','MarkerEdgeColor','green'); 

Introduced before R2006a

Was this topic helpful?