I have written a Matlab R2020b script that optionally receives three inputs – but if the inputs are not received, they are set to the defaults within the script.
function my_func(varargin)
user_inputs = inputParser;
addOptional(user_inputs, 'input1', getinput1());
addOptional(user_inputs, 'input2', getinput2());
addOptional(user_inputs, 'input3', getinput3());
parse(user_inputs, varargin{:});
input1 = user_inputs.Results.input1;
input2 = user_inputs.Results.input2;
input3 = user_inputs.Results.input3;
% definitions for the get input functions are here, but aren't important to my question
end
Here, the functions getinput1()
, getinput2()
, and getinput3()
are used to generate the appropriate default values.
My intent is to allow the user to call the function with or without the inputs. When I call the function without inputs, it works correctly, but when I call the function with all three inputs as
my_func('myfirstinput', 'mysecondinput', 'mythirdinput')
then I get the error:
Error using my_func
Too many input arguments
What is causing this error, and how do I correctly call a function while incorporating its optional parameters?