Bug in the parse, inputParser family of functions?

조회 수: 2 (최근 30일)
Peter
Peter 2021년 2월 5일
댓글: Cris LaPierre 2023년 5월 5일
I've stumbled upon what sure appears to me to be a bug in the parse and parseInput suite of functions. It seems to have to do with some combination of having character strings as arguments, having a valid function apply to check the validity of the arguments, and the order in which the arguements are provided. Here's perhaps the simpliest code to reproduce the issue:
tryOne = myParse('alpha','one', 'beta','two')
tryTwo = myParse('beta', 'two', 'alpha','one')
function results = myParse(varargin)
parseObj = inputParser;
addOptional(parseObj, 'alpha', 'none', @(x) ( isstring(x) || ischar(x) ) );
addOptional(parseObj, 'beta', 'none', @(x) ( isstring(x) || ischar(x) ) );
parse(parseObj,varargin{:})
results = parseObj.Results;
end
Note that the only difference between the first and second call to "myParse" is the order of the arguements. Running this little test code returns the wrong result in the first case:
tryOne =
struct with fields:
alpha: 'alpha'
beta: 'two'
Notice that 'alpha' is set to the string 'alpha' instead of being set to 'one', but 'beta' is set correctly. However, in the second call (tryTwo), with the order of the args just reversed, the correct result is returned.
TryTwo =
struct with fields:
alpha: 'one'
beta: 'two'
BTW, if I run the first try again, it gets the answer wrong again, so its not a matter of just the first call to parse returning the wrong answer.
I've tried all kinds of variants of the above, including numeric args, including or not including a validation check function in "addOptional", some of which seem to work correctly and some don't. But the example here seems to illustrate the issue most simply.
The parse/parseInput suite of functions are built-ins, so I can't dive into the code of those functions to figure out what is really going on here.
  댓글 수: 5
Peter
Peter 2021년 2월 8일
편집: Stephen23 2021년 2월 8일
This behavior was seen in both
i) R2020b Update 3 on a MacOS
ii) R2020a Update 1 on a linux (CentOS 7) platform.
Here's the output of ver from the MacOS machine:
----------------------------------------------------------------------------------------------------
MATLAB Version: 9.9.0.1538559 (R2020b) Update 3
MATLAB License Number: XXXXXXX
Operating System: macOS Version: 11.1 Build: 20C69
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.9 (R2020b)
Deep Learning Toolbox Version 14.1 (R2020b)
MATLAB Compiler Version 8.1 (R2020b)
Mapping Toolbox Version 5.0 (R2020b)
NCTOOLBOX Tools for read-only access to Common ... Version 1.1.1-13-g8582810++
Optimization Toolbox Version 9.0 (R2020b)
Statistics and Machine Learning Toolbox Version 12.0 (R2020b)
Stephen23
Stephen23 2021년 2월 8일
@Peter: I removed the license number from your comment. Best not to post that on the interweb.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2021년 2월 8일
편집: Matt J 2021년 2월 8일
Note that the only difference between the first and second call to "myParse" is the order of the arguements.
If the order is not supposed to matter, you should be using addParameter rather than addOptional,
tryOne = myParse('alpha','one', 'beta','two')
tryOne = struct with fields:
alpha: 'one' beta: 'two'
tryTwo = myParse('beta', 'two', 'alpha','one')
tryTwo = struct with fields:
alpha: 'one' beta: 'two'
function results = myParse(varargin)
parseObj = inputParser;
addParameter(parseObj, 'alpha', 'none', @(x) ( isstring(x) || ischar(x) ) );
addParameter(parseObj, 'beta', 'none', @(x) ( isstring(x) || ischar(x) ) );
parse(parseObj,varargin{:})
results = parseObj.Results;
end
  댓글 수: 4
Matt J
Matt J 2021년 2월 8일
Yes, that is very odd!
Cris LaPierre
Cris LaPierre 2023년 5월 5일
A little late to the party, but want to reiterate that you should use addParameter instead of addOptional because you intend to treat the inputs as name-value pairs instead of optional positional arguments.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by