InputParser - problem with keepUnmatched

조회 수: 1 (최근 30일)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2016년 9월 7일
답변: Jiro Doke 2017년 8월 15일
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
Eric Ng
Eric Ng 2017년 8월 15일
I have the same issue. It was working fine in Matlab 2015b but not in 2016b. Any fix by Mathworks?

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

답변 (2개)

Walter Roberson
Walter Roberson 2016년 9월 8일
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 2017년 8월 15일
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 ).

카테고리

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