Tutorial

Functions with Variable Input 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 varargin as an input argument in the definition of a function, and the function will be able to accept any number of inputs. The variable varargin must be placed after other input arguments, if they exist.

When the function executes, varargin is a cell array. It stores the inputs passed in the call, whose position is greater than or equal to the position of varargin in the input argument list.

function showinputargs1(varargin)
celldisp(varargin);
end
function showinputargs2(a,b,varargin)
a 
b 
celldisp(varargin);
end
% Call 'showinputargs1'
showinputargs1(5,'Hello World!',3);
% Call 'showinputargs2'
showinputargs2('MATLAB',2,7,10);

In the preceding code, both functions show their input arguments in the Command Window. Note that in the case of showinputargs2, the first two inputs are not stored in the variable varargin, because varargin is at the third position in the input argument list.

Example

The function computesum calculates the sum of its input arguments. It allows any number of inputs.

function S = computesum(varargin)
S = sum([varargin{:}]);
end
% Call 'computesum', passing zero inputs
resultExample1 = computesum();
% Call 'computesum', passing two inputs
resultExample2 = computesum(1,2);
% Call 'computesum', passing four inputs
resultExample3 = computesum(1,2,3,4);

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 varargin;

Source code

I hope you have learned how to create and use functions that support a variable number of inputs 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.