Function with Variable Number of Input Arguments in MATLAB


MATLAB is a high-level programming language that has capability to define a function that can accept a variable number of input arguments. In MATLAB, there is no need of defining any additional function or array to define a function with variable number of input arguments.

How to Create a Function with Variable Number of Input Arguments in MATLAB?

In MATLAB, when the keyword "varargin" is used in the function definition statement then it enables the function to take any number of input arguments. The "varargin" keyword is composed of "VARiable ARGument INput". It is important to note that the keyword "varargin" must be specified in lowercase characters. Also, the "varargin" must be declared as the last input argument in the function definition.

If the function executes,the "varargin" argument will create a 1-by-Ncell array. Here,is the number of inputs that the function takes after accepting the inputs declared explicitly. In case when the function takes no inputs after the inputs declared explicitly, the "varargin" argument will be an empty cell array.

Syntax

The following syntax is used to create a function with variable number of input arguments in MATLAB.

function MyFun(var1, var2, var3, …, varargin)
--- Function Body ---
end

Overall, in MATLAB, when we use the keyword 'varargin' in a function, then it makes the function flexible to accept any number of input arguments.

This kind of functions that can accept any number of input arguments are generally used in situations where we have to input arguments without defining them at the time of function creation.

Now, let us understand how we can use the "varargin" option in a MATLAB function to accept a variable number of input arguments.

First of all, we define a function and will specify the 'varargin' option as its one of the input arguments in the function definition statement as follows:

function MyFun(varargin)

After that we can take any number of input arguments inside the function body by using this 'varargin' variable, which will basically act as a cell array. This cell array will contain all the input arguments passed to the function during run time.

Further, we do not know in advance that how many input arguments will be supplied to the function. To overcome this issue, we can use syntaxes like 'numel(varargin)' or 'length(varargin)' to determine the number of input arguments that can be provided to the function.

This is all about the basics of creating a function with variable number of input arguments in MATLAB.

Example

Now, let us consider an example to practically understand how the 'varargin' option works.

% MATLAB code to create a function with variable number of input arguments
function TutorialsPoint(varargin)
    n = numel(varargin);
    disp(['Number of Input Arguments: ' num2str(n)]);
    
    for i = 1:n
        disp(['Input Argument ' num2str(i) ': ' num2str(varargin{i})]);
    end
end

After creating this function with name "TutorialsPoint" that can take any number of input arguments. Let us now call this function with different numbers of input arguments, as given below.

% Calling the function with different number of input arguments
TutorialsPoint('Tutorials', 'Point', 'is', 'a', 'Free', 'Resource', 'for', 'Learners.');
TutorialsPoint('MATLAB', 'Course', 'ID', 2001);
TutorialsPoint(500);

Output

Number of Input Arguments: 8
Input Argument 1: Tutorials
Input Argument 2: Point
Input Argument 3: is
Input Argument 4: a
Input Argument 5: Free
Input Argument 6: Resource
Input Argument 7: for
Input Argument 8: Learners.

Number of Input Arguments: 4
Input Argument 1: MATLAB
Input Argument 2: Course
Input Argument 3: ID
Input Argument 4: 2001

Number of Input Arguments: 1
Input Argument 1: 500

Explanation

In this MATLAB example, we first create a function (TutorialsPoint in this case) with the 'varargin' option as its input argument. Then, we define the function body. Finally, we save this function in our MATLAB workspace.

After that we call the function in the MATLAB command window with any number of input arguments. The function will accept the input arguments and will display a result as per the function definition.

Example

Let us now consider another example to create a function that can add any number of numeric input arguments.

function MySum(varargin)
    n = numel(varargin);
    S = 0;

 for i = 1:n
        S = S + varargin{i};
    end
  
disp(['Sum of input Numbers: ' num2str(S)]);
end

Now, let us call this function to add multiple numbers of numerical input arguments.

% Calling the function with different number of input arguments
MySum(1, 2, 3);
MySum(5, 10, 20, 50, 100);

Output

Sum of input Numbers: 6
Sum of input Numbers: 185

Conclusion

This is all about creating a function with variable number of input arguments in MATLAB. In MATLAB, we have a variable 'varargin' that we can use in a function to allow the function to accept any number of input arguments.

In this tutorial, we explained all the fundamental concepts related to the 'varargin' variable. We also included some example programs to make the concept easier to understand.

Updated on: 07-Sep-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements