Tutorial

How to Validate the Number of Input Arguments Passed to a Function in MATLAB

Use the functions nargin or narginchk to validate the number of inputs arguments passed to a function.

Description

MATLAB validates automatically the number of inputs passed to a function, according to the definition and the body of the function. For example, the function computesum1 only accepts three inputs and one output.

function S = computesum1(a,b,c)
S = a + b + c;
end
% Call 'computesum1', passing three inputs
sum1 = computesum1(1,2,3);
% Call 'computesum1', passing two inputs
sum2 = computesum1(1,2);
% Call 'computesum1', passing four inputs
sum3 = computesum1(1,2,3,4);

However, if a function supports a variable number of input arguments, the automatic validation is not possible. In this case, you should use the functions nargin or narginchk to validate the number of inputs passed to the function.

nargin

The statement nargin returns the number of input arguments passed to the currently executing function. It is used below to validate that only one or three inputs are passed to computesum2 in each call.

Learn here how to use logical operators in MATLAB.
function S = computesum2(varargin)
if((nargin ~= 1) && (nargin ~= 3))
    error('IPL:computesum2:WrongNumberOfInputs', 'This function only supports one or three inputs.');
end
S = sum([varargin{:}]);
end
% Call 'computesum2', passing one input
sum4 = computesum2(1);
% Call 'computesum2', passing two inputs
sum5 = computesum2(1,2);

narginchk

The statement narginchk(minargs,maxargs) throws an error if the number of input arguments passed to the currently executing function is not in the range \([minargs,maxargs]\). It is used below to validate that only one, two or three inputs are passed to computesum3 in each call.

Specify Inf as maxargs to accept an unlimited number of inputs.
function S = computesum3(varargin)
narginchk(1,3);
S = sum([varargin{:}]);
end
% Call 'computesum3', passing three inputs
sum6 = computesum3(1,2,3);
% Call 'computesum3', passing zero inputs
sum7 = computesum3();
% Call 'computesum3', passing four inputs
sum8 = computesum3(1,2,3,4);

Tip

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 pages in the MATLAB help documentation, and the other tutorials in this series.

doc nargin;
doc narginchk;

Exercise

Define a function called “dosomething” and ensure that it only accepts one, two or five input arguments. The function does not have outputs.

Source code

I hope you have learned how to validate the number of input arguments passed to a function 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.