Bug in the parse, inputParser family of functions?

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Peter
Peter il 8 Feb 2021
Modificato: Stephen23 il 8 Feb 2021
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 il 8 Feb 2021
@Peter: I removed the license number from your comment. Best not to post that on the interweb.

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 8 Feb 2021
Modificato: Matt J il 8 Feb 2021
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 Commenti
Cris LaPierre
Cris LaPierre il 5 Mag 2023
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Argument Definitions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by