How to dispatch optional arguments to nested functions
古いコメントを表示
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++?
採用された回答
その他の回答 (1 件)
Ramya Vulimiri
2019 年 3 月 7 日
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
カテゴリ
ヘルプ センター および File Exchange で Argument Definitions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!