Bird
Raised Fist0
MATLABdata~5 mins

Variable arguments (varargin, varargout) in MATLAB

Choose your learning style10 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.

Practice

(1/5)
1. What does varargin do in a MATLAB function?
easy
A. Returns multiple output arguments as a cell array
B. Collects all input arguments into a cell array
C. Limits the number of input arguments to one
D. Specifies the data type of input arguments

Solution

  1. Step 1: Understand the role of varargin

    varargin is used to collect any number of input arguments passed to a function into a single cell array.
  2. Step 2: Differentiate from varargout

    varargout is used for output arguments, not input. So it does not collect inputs.
  3. Final Answer:

    Collects all input arguments into a cell array -> Option B
  4. Quick Check:

    varargin = input collector [OK]
Hint: Remember: 'varargin' collects inputs, 'varargout' returns outputs [OK]
Common Mistakes:
  • Confusing varargin with varargout
  • Thinking varargin limits inputs
  • Assuming varargin specifies data types
2. Which of the following is the correct way to define a MATLAB function that accepts a fixed input followed by any number of additional inputs using varargin?
easy
A. function output = myFunc(input1, varargin)
B. function output = myFunc(varargout)
C. function output = myFunc(varargin)
D. function output = myFunc(varargin, input1)

Solution

  1. Step 1: Check correct varargin placement

    varargin must be the last input argument to collect all remaining inputs.
  2. Step 2: Evaluate options

    function output = myFunc(input1, varargin): function output = myFunc(input1, varargin) correctly places varargin after the fixed input input1. function output = myFunc(varargout) incorrectly uses varargout as an input. function output = myFunc(varargin) has no fixed input. function output = myFunc(varargin, input1) places varargin before a fixed input, which is invalid.
  3. Final Answer:

    function output = myFunc(input1, varargin) -> Option A
  4. Quick Check:

    varargin must be last input [OK]
Hint: Put varargin last in input list to accept extra arguments [OK]
Common Mistakes:
  • Placing varargin before fixed inputs
  • Using varargout as input
  • Omitting parentheses in function definition
3. What will be the output of the following MATLAB code?
function varargout = exampleFunc(varargin)
  n = length(varargin);
  for k = 1:n
    varargout{k} = varargin{k} * 2;
  end
end

[a, b] = exampleFunc(3, 5); disp([a, b]);
medium
A. Error: varargout not assigned properly
B. [3 5]
C. [6 10]
D. [9 25]

Solution

  1. Step 1: Understand varargin and varargout usage

    varargin collects inputs {3, 5}. The loop doubles each input and assigns to varargout.
  2. Step 2: Calculate output values

    3 * 2 = 6, 5 * 2 = 10. So outputs are 6 and 10.
  3. Final Answer:

    [6 10] -> Option C
  4. Quick Check:

    Each input doubled = output [OK]
Hint: varargin inputs, varargout outputs; loop doubles inputs here [OK]
Common Mistakes:
  • Confusing input and output values
  • Forgetting to assign varargout elements
  • Assuming varargout is a numeric array, not cell
4. Identify the error in this MATLAB function using varargin:
function result = sumAll(varargin)
  total = 0;
  for i = 1:varargin
    total = total + varargin{i};
  end
  result = total;
end
medium
A. Incorrect indexing of varargin with parentheses
B. Missing output argument varargout
C. No error, function works correctly
D. Using varargin as a number in the for loop

Solution

  1. Step 1: Check for loop range

    The for loop uses 1:varargin, but varargin is a cell array, not a number.
  2. Step 2: Correct way to get number of inputs

    Use length(varargin) or nargin to get number of inputs for the loop.
  3. Final Answer:

    Using varargin as a number in the for loop -> Option D
  4. Quick Check:

    Loop needs numeric length, not cell array [OK]
Hint: Use length(varargin) to loop over inputs, not varargin itself [OK]
Common Mistakes:
  • Using varargin directly as a number
  • Forgetting to use curly braces for cell access
  • Assuming varargout is required for outputs
5. You want to write a MATLAB function that accepts any number of numeric inputs and returns their squares as multiple outputs. Which function definition and body correctly uses varargin and varargout to achieve this?
hard
A. function varargout = squareAll(varargin) for k = 1:length(varargin) varargout{k} = varargin{k}^2; end end
B. function varargout = squareAll(varargin) for k = 1:nargin varargout{k} = varargin(k)^2; end end
C. function varargout = squareAll(varargin) for k = 1:length(varargin) varargout(k) = varargin{k}^2; end end
D. function varargout = squareAll(varargin) for k = 1:nargout varargout{k} = varargin{k}^2; end end

Solution

  1. Step 1: Check input count method

    Using length(varargin) correctly gets the number of inputs. nargin inside the function counts all inputs including fixed ones, but here only varargin inputs matter.
  2. Step 2: Check cell indexing and assignment

    Accessing inputs with varargin{k} and assigning outputs with varargout{k} using curly braces is correct. Parentheses would cause errors.
  3. Final Answer:

    function varargout = squareAll(varargin) for k = 1:length(varargin) varargout{k} = varargin{k}^2; end end -> Option A
  4. Quick Check:

    Use length(varargin) and curly braces for cell access [OK]
Hint: Use length(varargin) and curly braces {} for cell arrays [OK]
Common Mistakes:
  • Using parentheses instead of curly braces for cell access
  • Using nargin instead of length(varargin)
  • Assigning outputs with parentheses instead of curly braces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard