InputParser - problem with keepUnmatched

5 views (last 30 days)
Ex: function data(input1,input2)
where input1 = 'AUDI'
input2 = 1
I have used the input parser to check the inputs
a = inputParser;
a.KeepUnmatched = true;
addRequired(a,'input1',@ischar);
addRequired(a,'input2',@ischar);
parse(a,input1,input2);
Its working fine and detecting the problem too. But in the above program i have a set the keepUnmatched field as ''true''. So now when the error pops up, it should be collected in the ''unmatched'' field of parser actually and matlab should supress the error too.
But in my case as the input2 is false matlab throwing the error but the error is not collected in the Unmatched field. How to fix this problem ?
Thanks a lot
  1 Comment
Eric Ng
Eric Ng on 15 Aug 2017
I have the same issue. It was working fine in Matlab 2015b but not in 2016b. Any fix by Mathworks?

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 8 Sep 2016
I disagree. You asked for two required key/value pairs, one with name literally 'input1' and the other with name literally 'input2', and you asked the the corresponding value for each be of type character. You then ask to parse upon input 'AUDI', 1 . The strings 'input1' and 'input2' are not present, so an error needs to be generated .
If the user had passed in 'input2', 'hello' then that would have matched the required parameter 'input2' but would have failed because it was missing the required 'input1'
You will never be able to match two mandatory property/value pairs without allowing at least four inputs instead of two.
The KeepUnmatched does not override the need for mandatory parameters: instead it allows you to get clean control of additional parameters after all mandatory and optional parameters have been handled.
Your code is confusing to users because you use the variable names input1 and input2 as well as happening to look for the literal strings 'input1' and 'input2', but those are different domains. It's like naming your cat "dog" and then calling out "Here dog!" and expecting your cat to know the difference between that and "Here 'dog'!" (in the first of those you are calling for the dog, but in the second of those you are calling for the thing named 'dog', which is a cat.)

Jiro Doke
Jiro Doke on 15 Aug 2017
The KeepUnmatched property is for optional parameter-value inputs, not for the required inputs. As Walter mentioned, for this to work, you need to provide "pairs" of inputs, and make them optional param-value inputs ( addParameter ).

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!