MATLAB: Is there a simple way to condense the following codes

function input argumentMATLABoption pricing

function greek=OptionGreeks(opt,arg1,arg2,arg3,arg4,arg5)
seed=rng;
if nargin==3
greek1=opt.opt1.OptionGreeks(arg1, arg2);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2);
elseif nargin==4
greek1=opt.opt1.OptionGreeks(arg1, arg2, arg3);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2, arg3);
elseif nargin==5
greek1=opt.opt1.OptionGreeks(arg1, arg2, arg3, arg4);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2, arg3, arg4);
else
greek1=opt.opt1.OptionGreeks(arg1, arg2, arg3, arg4, arg5);
rng(seed);
greek2=opt.opt2.OptionGreeks(arg1, arg2, arg3, arg4, arg5);
end
greek=greek1-greek2;
end
Above are my codes. opt is a SpreadOption class that has properties opt1 and opt2, which are Option class. Function SpreadOption.OptionGreeks() calls function Option.OptionGreeks() that accepts different number of input arguments.
I'm wondering if there's a way to condense the codes.
Also, I'm wondering what the easiest way is to customize my input argument, like opt.OptionPrice(S=3610, method='binomial tree', brunches=100000)? (just like python and R do)?It seems not convenient since MATLAB cannot overload/override a certain function.

Best Answer

function greek = OptionGreeks(opt, varargin)
if length(varargin) > 5; varargin = varargin(1:5); end
seed = rng;
greek1=opt.opt1.OptionGreeks(varargin{:});
rng(seed);
greek2=opt.opt2.OptionGreeks(varargin{:});
greek=greek1-greek2;