How to Validate Number of Function Arguments in MATLAB?
Last Updated :
28 Apr, 2025
MATLAB functions are generally defined input and output arguments. To validate the number of these input-output arguments, MATLAB provides us with the following functions:
- narginchk
- nargoutchk
Syntax:
narginchk(<minimum-inputs>, <maximum-inputs>)
nargoutchk(<minimum-outputs>, <maximum-outputs>)
We use narginchk and nargoutchk to manually handle the exceptions generated by a lesser or greater number of arguments during a function call. In the following sections, we will see the usage of both functions with examples.
NARGINCHK in MATLAB
As discussed above, narginchk is used to validate the number of input arguments. We shall see an example where, if the input arguments in function calls are in range 2,10 then, the function prints them all else, narginchk throws an error.
Example 1:
Matlab
% MATLAB Code
fun(2,3,4)
fprintf("Lees input variables!\n\n")
fun()
% function
function fun(varargin)
min = 2;
max = 10;
narginchk(min,max)
disp(varargin)
end
Output:
In this code, the function fun takes a variable number of input arguments with the help of the varargin argument. Then, the narginchk function checks whether the number of passed input arguments is in the range 2,10 or not. If true then, the function displays the variable argument list.
As it can be seen in the output, the first function call with 3 (>2 & <10) arguments print the argument list however, the second function call throws an error as the number of arguments is 0.
NARGOUTCHK in MATLAB
Similarly, we can validate the number of output arguments with the help of the nargoutchk function. narginchk throws an error when a number of output arguments is not in the defined range. See the following example where we assign a random number to each output argument whenever the number of output arguments is from 3 to 9.
Example 2:
Matlab
% MATLAB Code
[w,x,y,z] = fun();
disp([w x y z])
fprintf("Less output variables!\n\n")
fun()
%function
function [varargout] = fun
min = 3;
max = 9;
nargoutchk(min,max)
fprintf('Number of requested outputs is %d\n',nargout)
varargout = cell(nargout,1);
for i = 1:nargout
varargout{i} = rand;
end
end
Output:
In the above code, we define a function that assigns a random number to all of its output arguments and displays the number of output arguments, using the varargout for a variable number of outputs. The nargoutchk checks whether the number of output arguments is in the range 3,9. If not, it throws an error. So the first function call with 4 output arguments works as expected whereas the second call with no output arguments throws an error.
Similar Reads
How to Find Number of Function Arguments in MATLAB? The number of function arguments passed in MATLAB will be determined in the following article. Unlike C, C++, and Java, MATLAB can accommodate a variable amount of parameters provided into a function without throwing an error. We'll go through how we determine the actual amount of parameters supplie
4 min read
Function With Variable Number of Input Arguments in MATLAB MATLAB functions have the functionality that many other programming languages lack. A MATLAB function can take a variable number of input arguments, without the use of arrays or any additional functionality. The way to go about this in MATLAB is by using the varargin - which can be interpreted as VA
2 min read
Function Argument Validation in MATLAB The basics of Function Argument is, " The functions receive certain inputs from the calling command which applies in function syntax and processes it. sometimes It may or may not produce output argument it dependence completely on the functions efficiency or the accuracy of your code." Function Argu
5 min read
How to Validate Input to a Function Error in R In this article, we will examine various methods for how to validate input to the function by using R Programming Language. How to validate input to the function?Validating input to a function in R is crucial for ensuring that your code behaves as expected and can handle various types of input grace
5 min read
How To Plot a Function of Two Variables in MATLAB? In order to plot a function of two variables in Matlab first you have to know some functions like plot, meshgrid() function, and very well know about how to plot one variable functions in Matlab. You have to follow some main contents or you can say procedure to plot a function. Below is the process
2 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read