Check the documentation on varargin
and nargin
.
Basically, do something like
function out = my_func(a,varargin)
if nargin == 1
b = 2; % Your default value
elseif nargin == 2
b = varargin{1};
end
Note that the above does mean you have to have a fixed order of input arguments. Any arguments explicitly named in the function declaration, a
in this case, always have to be present, and anything within the varargin
has to be in the set order, e.g. you can add a c = varargin{2}
, then you cannot set c
without setting b
.
If you want to be able to give Python-like input argument, i.e. regardless of the order, you need name-value pairs. This is done via inputParser
, as suggested in Cris Luengo's answer