Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using InputParse for boolean field

47 views
Skip to first unread message

elgen

unread,
Aug 27, 2010, 11:06:52 AM8/27/10
to
I am trying to add a flag called "debug" as an optional input parameter
to the function. The test function is listed below:

function untitled(a, varargin)
p = inputParser;
p.addRequired('a', @isnumeric);
p.addOptional('debug', 0, @(x)strcmpi(x, 'debug'));
p.parse(a, varargin{:})
disp(p.Results)


If i type

>> untitled(3)

a: 3
debug: 0


If i type

>> untitled(3, 'debug')

a: 3
debug: 'debug'

I want Matlab to place 1 in the debug field, i.e.

>> untitled(3, 'debug')

a: 3
debug: 1

Is it possible? Do I need to play with the anonymous function handle?
FYI, matlab doc says that if the Boolean output is true, then MATLAB
accepts the input value. I want to work around this.

Thank you.

Loren Shure

unread,
Aug 30, 2010, 7:32:59 AM8/30/10
to

"elgen" <ske...@no.spam.hotmail.com> wrote in message
news:i58kar$hru$1...@news.eternal-september.org...


I think you're validator is checking for the string debug rather than
validating its value.

--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

rmc256

unread,
Jun 17, 2011, 10:38:04 AM6/17/11
to
I'd like to revive this thread, as I don't understand if the original question is possible or not.
Can I make an optional input to the inputParser (addOptional) that will return a 1 if the string is present in the function call, or a 0 if not? If so how? It seems Matlab will always take the string itself to be the value. I would have to pass a 1, but that's not very clear syntax.
If there is not a way to do this, I could do it as a parameter value pair (i.e. untitled(3,'debug',1) ). Seems unnecessary.

Michael


"Loren Shure" <loren...@mathworks.com> wrote in message <i5g4tb$j6p$1...@fred.mathworks.com>...

Maria

unread,
Apr 23, 2012, 6:36:07 PM4/23/12
to
"rmc256" wrote in message <itfosc$phe$1...@newscl01ah.mathworks.com>...
If you want 'debug' to be a boolean variable, why not do this:

function untitled(a, varargin)
p = inputParser;
p.addRequired('a', @isnumeric);
p.addParamValue('debug',false,@islogical);
p.parse(a, varargin{:})
disp(p.Results)

Then
>> untitled(3)
a: 3
debug: 0

>> untitled(3,'debug',true)
a: 3
debug: 1
0 new messages