help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help-octave Digest, Vol 99, Issue 47


From: Carnë Draug
Subject: Re: Help-octave Digest, Vol 99, Issue 47
Date: Sat, 21 Jun 2014 02:59:58 +0100

On 17 June 2014 09:04, Damian <address@hidden> wrote:
> Hello,
>
> Recently I run into some unexpected (for me) behavior when using
> inputParser. Here is the example:
>
>     p = inputParser;
>     p.FunctionName = 'my_function;
>     p.CaseSensitive = true;
>     p.KeepUnmatched = true;
>
>     p = p.addOptional('local', 0, @isnumeric);
>
>     test = {'local', 15};
>
>     res = p.parse(test{:})
>
> After executing this code, the parameter 'local' appears as unmatched,
> even though is passed as parameter.
>
> If I change the KeepUnmatched attribute to 'false' I get an error
> saying that an unmatched parameter was found (namely 'local').
>
> I would expect the 'local' parameter specified in 'test' to be parsed
> without a problem. Am I missing something?
>
> I'm using Octave version 3.6.4.
>
> Thanks in advance,
> Damian.

I think you are misunderstanding what Optional means and what you
should be using is ParamValue. Consider imread:

imread ("thisfile.tif", "tiff", "index", 1:10)

the first argument is required, second argument is optional, and the
third and fourth are key and respective value. It's the same as:

imread ("thisfile.tif", "index", 1:10)

In your example, you are setting an optional argument named "local"
that defaults to 0. The name for an "optional" argument is only used
so you can access the elements from the parsed object. This means that
with your set up, if you use

foo ("local", 15)

inputParser will check if the first argument passes the check for the
optional argument. It fails, because the test is @isnumeric, but the
value is the string "local" (so inputParser is correct to fail). If
you set it to KeepUnmatched, then it decides that it an undeclared
param/value pair of "local" with a value of 15 (still correct).

Basically, you said it yourself "After executing this code, the
parameter 'local' appears as unmatched, even though is passed as
parameter.". Yes, it is passed as parameter, but you did not set
inpurParser to look for a parameter name that has a matching value
(ParamValue). You set it to look for an optional value. The usage of
an Optional in inputParser is diminished in Octave because you can
already set the defaults as in:

function foo (val = 15)
  ...
endfunction

Carne



reply via email to

[Prev in Thread] Current Thread [Next in Thread]