How to dispatch optional arguments to nested functions

6 views (last 30 days)
A main function receives a variable number of arguments. It then needs to call other functions that can depend on these optional inputs.
I want to assign a meaningful default value and check the validity of the optional parameter (parameter B in the following example) only in the function that actually uses it (fctB). Unfortunately, I have to assign a value to B in the main function, when I extract it from varargin. At that point the input parser pB considers that parameter B is being passed to fctB and will not use the default value (even though this is what I want).
Here is a code that illustrates the problem:
function dispatch(varargin)
p = inputParser;
paramName = 'A';
defaultVal = {};
addOptional(p,paramName,defaultVal);
paramName = 'B';
defaultVal = {};
addOptional(p,paramName,defaultVal);
paramName = 'C';
defaultVal = {};
addParameter(p,paramName,defaultVal);
p.parse(varargin{:});
B = p.Results.B;
fctB(B);
end
function fctB(varargin)
pB = inputParser;
paramName = 'B';
defaultVal = 10;
validationFcn = @(x) validateattributes(x,{'numeric'},{'scalar','positive','integer'});
addOptional(pB,paramName,defaultVal,validationFcn);
pB.parse(varargin{:});
B = pB.Results.B;
disp(['B = ',num2str(B)]);
end
Calling the function using, for instance, the value B = 2 produces the correct result
dispatch(1,2)
But calling it without arguments will throw an error due to the validation function for B.
Is there a way to pass an argument to a function in such a way that the input parser for that function recognizes that the argument has not been initialized and uses the default value instead? I guess I am looking for something equivalent to the content of an empty cell array {:}.
If this does not exist in MATLAB, how is such a problem resolved in other languages such as C++?

Accepted Answer

Julien Renard
Julien Renard on 11 Mar 2019
Sumitting my previous comment as an answer to close the question:
[...] As a solution, I have implemented a subclass that overloads some of the methods of the inputParser class. As a result, if the input argument satisfies some criteria, the input parser validation function is bypassed and the default value is assigned.

More Answers (1)

Ramya Vulimiri
Ramya Vulimiri on 7 Mar 2019
Hi Julien,
I understand you are trying to pass an optional argument to a function, which processes the argument if it has been initialised and if not, processes a default value for the argument.
If you would be open to the idea of setting the defaults for the parameters in the main function, please refer to this blog on a "Nice Wat to set Function Defaults". A snippet from the blog:
% set defaults for optional inputs
optargs = {eps 17 @magic};
% now put these defaults into the valuesToUse cell array,
% and overwrite the ones specified in varargin.
optargs(1:numvarargs) = varargin;
In this way, there is only one place to set/change the default values (adding additional optional arguments and their default values will also be easy) and the validation function inside fctB will get the default value or the actual argument to be validated.
If you are looking for argument checking in nested functions, you can try using nargin on the main function. Please refer here for more information: https://www.mathworks.com/help/matlab/matlab_prog/argument-checking-in-nested-functions.html
  1 Comment
Julien Renard
Julien Renard on 7 Mar 2019
Hi Ramya,
Thank you for your suggestion, but unfortunately, I do not think that it can solve my problem.
If the optional argument is not passed to the function, its default value is computed based on other parameters of the function computed at runtime. Trying to set the default value outside of the function is not practical.
As a solution, I have implemented a subclass that overloads some of the methods of the inputParser class. As a result, if the input argument satisfies some criteria, the input parser validation function is bypassed and the default value is assigned.

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!