0
0
MATLABdata~5 mins

Variable arguments (varargin, varargout) in MATLAB

Choose your learning style9 modes available
Introduction

Sometimes you want a function to accept any number of inputs or give back any number of outputs. Variable arguments let you do that easily.

When you want a function to handle different numbers of inputs without writing many versions.
When you want to return different amounts of results depending on the situation.
When you write a flexible tool that can adapt to many cases.
When you don't know in advance how many inputs or outputs will be needed.
When you want to simplify your code by avoiding many input checks.
Syntax
MATLAB
function [varargout] = myFunction(varargin)
    % varargin is a cell array of inputs
    % varargout is a cell array of outputs
    % Use varargin{i} to access inputs
    % Assign outputs to varargout{i}
end

varargin collects all extra inputs into a cell array.

varargout lets you return multiple outputs as a cell array.

Examples
This function adds any number of inputs given.
MATLAB
function result = sumAll(varargin)
    % sums all inputs
    result = 0;
    for k = 1:length(varargin)
        result = result + varargin{k};
    end
end
This function returns two outputs: minimum and maximum of all inputs.
MATLAB
function [minVal, maxVal] = minMax(varargin)
    % returns min and max of inputs
    values = [varargin{:}];
    minVal = min(values);
    maxVal = max(values);
end
This function returns up to three outputs: the input, its square, and its cube.
MATLAB
function varargout = multipleOutputs(x)
    varargout{1} = x;
    varargout{2} = x^2;
    varargout{3} = x^3;
end
Sample Program

This program shows how to use variable input and output arguments in MATLAB functions. It sums inputs, finds min and max, and returns multiple outputs.

MATLAB
function demoVarArgs()
    % Call sumAll with different numbers of inputs
    s1 = sumAll(1, 2, 3);
    s2 = sumAll(10, 20);
    disp(['Sum of 1,2,3 is: ' num2str(s1)])
    disp(['Sum of 10,20 is: ' num2str(s2)])

    % Call minMax with several inputs
    [mn, mx] = minMax(5, 3, 9, 1);
    disp(['Min is: ' num2str(mn) ', Max is: ' num2str(mx)])

    % Call multipleOutputs
    [a, b, c] = multipleOutputs(4);
    disp(['Input: ' num2str(a) ', Square: ' num2str(b) ', Cube: ' num2str(c)])
end

function result = sumAll(varargin)
    result = 0;
    for k = 1:length(varargin)
        result = result + varargin{k};
    end
end

function [minVal, maxVal] = minMax(varargin)
    values = [varargin{:}];
    minVal = min(values);
    maxVal = max(values);
end

function varargout = multipleOutputs(x)
    varargout{1} = x;
    varargout{2} = x^2;
    varargout{3} = x^3;
end
OutputSuccess
Important Notes

Inside the function, varargin is a cell array, so use curly braces {} to access elements.

Similarly, assign outputs to varargout using curly braces.

You can check nargin and nargout to know how many inputs or outputs were given.

Summary

Variable arguments let functions accept any number of inputs or outputs.

varargin collects inputs as a cell array; varargout returns outputs as a cell array.

This makes your functions flexible and easier to reuse.