Hi Sabhrant,
I understand that you are getting an error while using the “addOptional” function and are seeking an easire way to perform the task.
Please note that in MATLAB, the “addOptional” function in the “inputParser” class is ‘case-insensitive’ for the argument names. This means that if you try to add an optional argument with a name that is already defined (ignoring case), you will get an error.
To change this behavior and make the argument names case-sensitive, you can use the “addParameter” function instead of “addOptional”. The “addParameter” function was introduced in MATLAB R2013b and supports case-sensitive argument names. You refer to this documentation for more information: Add optional name-value pair argument into input parser scheme - MATLAB addParameter - MathWorks India
The following code snippet demonstrates the use of “addParameter”:
function S = test(varargin)
p = inputParser;
addRequired(p,'a',@isnumeric);
addOptional(p,'b',[],@isnumeric);
addParameter(p,'A',1,@isnumeric); % Use addParameter instead of addOptional
p.KeepUnmatched = true;
parse(p,varargin{:})
a = p.Results.a;
if isempty(p.Results.b)
b = 2*a;
else
b = p.Results.b;
end
A = p.Results.A;
S = a+b+A;
end
Hope this helps.