I always do like that:
if ~exist('arg1','var')
arg1=0;
end
As said by @Andrey, with this solution you can change the number/order of the arguments of the function, without changing the code. This is not the case with the nargin
solution.
As said by @yuk, if you want to allow to skip arguments you can do:
if ~exist('arg1','var') || isempty(arg1)
arg1=arg1DefaultValue;
end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…