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.
Variable arguments (varargin, varargout) in 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}
endvarargin collects all extra inputs into a cell array.
varargout lets you return multiple outputs as a cell array.
function result = sumAll(varargin)
% sums all inputs
result = 0;
for k = 1:length(varargin)
result = result + varargin{k};
end
endfunction [minVal, maxVal] = minMax(varargin)
% returns min and max of inputs
values = [varargin{:}];
minVal = min(values);
maxVal = max(values);
endfunction varargout = multipleOutputs(x)
varargout{1} = x;
varargout{2} = x^2;
varargout{3} = x^3;
endThis program shows how to use variable input and output arguments in MATLAB functions. It sums inputs, finds min and max, and returns multiple outputs.
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;
endInside 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.
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
varargin do in a MATLAB function?Solution
Step 1: Understand the role of varargin
vararginis used to collect any number of input arguments passed to a function into a single cell array.Step 2: Differentiate from varargout
varargoutis used for output arguments, not input. So it does not collect inputs.Final Answer:
Collects all input arguments into a cell array -> Option BQuick Check:
varargin = input collector [OK]
- Confusing varargin with varargout
- Thinking varargin limits inputs
- Assuming varargin specifies data types
varargin?Solution
Step 1: Check correct varargin placement
vararginmust be the last input argument to collect all remaining inputs.Step 2: Evaluate options
function output = myFunc(input1, varargin):function output = myFunc(input1, varargin)correctly placesvararginafter the fixed inputinput1. function output = myFunc(varargout) incorrectly usesvarargoutas an input. function output = myFunc(varargin) has no fixed input. function output = myFunc(varargin, input1) placesvararginbefore a fixed input, which is invalid.Final Answer:
function output = myFunc(input1, varargin) -> Option AQuick Check:
varargin must be last input [OK]
- Placing varargin before fixed inputs
- Using varargout as input
- Omitting parentheses in function definition
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]);Solution
Step 1: Understand varargin and varargout usage
varargincollects inputs {3, 5}. The loop doubles each input and assigns tovarargout.Step 2: Calculate output values
3 * 2 = 6, 5 * 2 = 10. So outputs are 6 and 10.Final Answer:
[6 10] -> Option CQuick Check:
Each input doubled = output [OK]
- Confusing input and output values
- Forgetting to assign varargout elements
- Assuming varargout is a numeric array, not cell
varargin:function result = sumAll(varargin)
total = 0;
for i = 1:varargin
total = total + varargin{i};
end
result = total;
endSolution
Step 1: Check for loop range
The for loop uses1:varargin, butvararginis a cell array, not a number.Step 2: Correct way to get number of inputs
Uselength(varargin)ornarginto get number of inputs for the loop.Final Answer:
Using varargin as a number in the for loop -> Option DQuick Check:
Loop needs numeric length, not cell array [OK]
- Using varargin directly as a number
- Forgetting to use curly braces for cell access
- Assuming varargout is required for outputs
varargin and varargout to achieve this?Solution
Step 1: Check input count method
Usinglength(varargin)correctly gets the number of inputs.nargininside the function counts all inputs including fixed ones, but here only varargin inputs matter.Step 2: Check cell indexing and assignment
Accessing inputs withvarargin{k}and assigning outputs withvarargout{k}using curly braces is correct. Parentheses would cause errors.Final Answer:
function varargout = squareAll(varargin) for k = 1:length(varargin) varargout{k} = varargin{k}^2; end end -> Option AQuick Check:
Use length(varargin) and curly braces for cell access [OK]
- Using parentheses instead of curly braces for cell access
- Using nargin instead of length(varargin)
- Assigning outputs with parentheses instead of curly braces
Ready to prove it?
Pass the test and this pattern moves unknown
