Tutorial

Functions with Variable Output Arguments in MATLAB

MATLAB provides a robust mechanism to define functions that should accept a variable number of inputs and outputs.

Description

Include the variable varargout as an output argument in the definition of a function, and the function will be able to accept any number of outputs. The variable varargout must be placed after other output arguments, if they exist.

When the function executes, varargout is a cell array. Use it to assign the outputs requested in the call, whose position is greater than or equal to the position of varargout in the output argument list.

nargout returns the number of requested outputs. Learn more here.
function varargout = filloutputs1()
for i = 1:nargout
    varargout{i} = ['Varargout ',num2str(i)]; 
end
end
function [a,b,varargout] = filloutputs2()
a = 'Output 1'; b = 'Output 2';
for i = 1:(nargout - 2)
    varargout{i} = ['Varargout ',num2str(i)]; 
end
end
% Call 'filloutputs1'
[a1,b1,c1,d1] = filloutputs1();
% Call 'filloutputs2'
[a2,b2,c2,d2] = filloutputs2();

In the preceding code, both functions accept a variable number of output arguments. Note that in the case of filloutputs2, the first two outputs are not assigned through the variable varargout, because varargout is at the third position in the output argument list.

Example

The function samplefunc allows any number of outputs, where the value of each output is the square of its position.

function varargout = samplefunc()
for i = 1:nargout
    varargout{i} = i * i; 
end
end
% Call 'samplefunc', requesting two outputs
[a3,b3] = samplefunc();
% Call 'samplefunc', requesting three outputs
[a4,b4,c4] = samplefunc();

Documentation

The functions that allow a variable number of inputs and outputs should be documented in detail, because the set of supported inputs and outputs is not stated explicitly in the function declaration statement.

In most cases, the user just sees an input argument called “varargin” or an output argument called “varargout”. If the user does not know the meaning of these variables, the situation is even worse.

Learn here how to add help documentation to a function in MATLAB.

Further reading

I recommend the following books to learn more on functions in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a Textbook Excellence Award. The sixth chapter covers user-defined functions, scope of variables, debugging, and other aspects related to the development of MATLAB programs.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Chapter 6 explains how to create functions, including anonymous functions, local functions, and nested functions.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

doc varargout;

Source code

I hope you have learned how to create and use functions that support a variable number of outputs in MATLAB. The source code developed in this tutorial is available at this page.

Share

Your friends may need this knowledge to complete a task. Share this tutorial.