Thread Subject: inputParser not validating serial object correctly

Subject: inputParser not validating serial object correctly

From: "G.A.M.

Date: 23 Sep, 2007 01:42:44

Message: 1 of 6

My error msg is:
Argument 'serialPort' failed validation @(sp)isa(sp,'serial').

My code is:

function status = DoSomethingWithSerialPort(serialPort, varargin)

p = inputParser;
p.addRequired('serialPort', @(sp)isa(sp, 'serial'));
p.addParamValue('test','', @ischar);
p.parse('serialPort', varargin{:});
test= p.Results.test;
       %etc.

After getting the error, I checked the class of the function
parameter, and it was as expected:
class(serialPort)
ans = serial

Then, to continue debugging, I created this anonymous function in the
workspace:
x = @(sp)isa(sp, 'serial')

I used it to check the input parameter and it was again as expected:
x(serialPort)
ans = 1

So does anyone have any idea why I get the error shown at the top?
Thanks.

Subject: inputParser not validating serial object correctly

From: Philip Borghesani

Date: 24 Sep, 2007 14:08:17

Message: 2 of 6

You have the inputs to parse wrong try:
p.parse(varargin{:});
or
parse(p,varargin{:})

Phil

"G.A.M." <x0Zero@gmail.com> wrote in message news:1190511764.536671.125930@y42g2000hsy.googlegroups.com...
> My error msg is:
> Argument 'serialPort' failed validation @(sp)isa(sp,'serial').
>
> My code is:
>
> function status = DoSomethingWithSerialPort(serialPort, varargin)
>
> p = inputParser;
> p.addRequired('serialPort', @(sp)isa(sp, 'serial'));
> p.addParamValue('test','', @ischar);
> p.parse('serialPort', varargin{:});
> test= p.Results.test;
> %etc.
>
> After getting the error, I checked the class of the function
> parameter, and it was as expected:
> class(serialPort)
> ans = serial
>
> Then, to continue debugging, I created this anonymous function in the
> workspace:
> x = @(sp)isa(sp, 'serial')
>
> I used it to check the input parameter and it was again as expected:
> x(serialPort)
> ans = 1
>
> So does anyone have any idea why I get the error shown at the top?
> Thanks.
>


Subject: Is Matlab Help incorrect Re: inputParser (parser is not validating serial object correctly)

From: "G.A.M.

Date: 25 Sep, 2007 15:55:02

Message: 3 of 6


>
> "G.A.M." <x0Z...@gmail.com> wrote in messagenews:1190511764.536671.125930@y42g2000hsy.googlegroups.com...
> > My error msg is:
> > Argument 'serialPort' failed validation @(sp)isa(sp,'serial').
>
> > My code is:
>
> > function status = DoSomethingWithSerialPort(serialPort, varargin)
>
> > p = inputParser;
> > p.addRequired('serialPort', @(sp)isa(sp, 'serial'));
> > p.addParamValue('test','', @ischar);
> > p.parse('serialPort', varargin{:});
> > test= p.Results.test;
> > %etc.
>
> > After getting the error, I checked the class of the function
> > parameter, and it was as expected:
> > class(serialPort)
> > ans = serial
>
> > Then, to continue debugging, I created this anonymous function in the
> > workspace:
> > x = @(sp)isa(sp, 'serial')
>
> > I used it to check the input parameter and it was again as expected:
> > x(serialPort)
> > ans = 1
>
> > So does anyone have any idea why I get the error shown at the top?
> > Thanks.


On Sep 24, 10:08 am, "Philip Borghesani"
<philip_borghes...@mathworks.spam> wrote:
> You have the inputs to parse wrong try:
> p.parse(varargin{:});
> or
> parse(p,varargin{:})
>
> Phil

Phil-thanks for your reply.
Matlab help on input parser gives this example:

function publish_ip(script, varargin)
p = inputParser;
p.addRequired('script', @ischar);
p.addOptional(...
[snip]
p.parse(script, varargin{:});

That's the example I'm following and I have used this style in a lot
of places in my code where it has seemingly worked correctly.

What I took from the example is that required arguments need to be
named in the call to parse().

1. Is the example code I posted originally an incorrect implementation
of what is shown in Matlab help?
2. Am I misunderstanding the example?
3. Is the help example incorrect?

Here's the help section I'm viewing:
inputParser :: Functions (MATLAB Function Reference)
help/techdoc/help.jar!/ref/inputparser.html


Subject: Is Matlab Help incorrect Re: inputParser (parser is not validating serial object correctly)

From: G.A.M.

Date: 2 Oct, 2007 14:56:18

Message: 4 of 6

 "G.A.M." <x0Zero@gmail.com> wrote in message
<1190735702.705426.76450@50g2000hsm.googlegroups.com>...
>
> >
> > "G.A.M." <x0Z...@gmail.com> wrote in
messagenews:1190511764.536671.125930@y42g2000hsy.googlegroups.com...
> > > My error msg is:
> > > Argument 'serialPort' failed validation
@(sp)isa(sp,'serial').
> >
> > > My code is:
> >
> > > function status =
DoSomethingWithSerialPort(serialPort, varargin)
> >
> > > p = inputParser;
> > > p.addRequired('serialPort', @(sp)isa(sp, 'serial'));
> > > p.addParamValue('test','', @ischar);
> > > p.parse('serialPort', varargin{:});
> > > test= p.Results.test;
> > > %etc.
> >
> > > After getting the error, I checked the class of the
function
> > > parameter, and it was as expected:
> > > class(serialPort)
> > > ans = serial
> >
> > > Then, to continue debugging, I created this anonymous
function in the
> > > workspace:
> > > x = @(sp)isa(sp, 'serial')
> >
> > > I used it to check the input parameter and it was
again as expected:
> > > x(serialPort)
> > > ans = 1
> >
> > > So does anyone have any idea why I get the error shown
at the top?
> > > Thanks.
>
>
> On Sep 24, 10:08 am, "Philip Borghesani"
> <philip_borghes...@mathworks.spam> wrote:
> > You have the inputs to parse wrong try:
> > p.parse(varargin{:});
> > or
> > parse(p,varargin{:})
> >
> > Phil
>
> Phil-thanks for your reply.
> Matlab help on input parser gives this example:
>
> function publish_ip(script, varargin)
> p = inputParser;
> p.addRequired('script', @ischar);
> p.addOptional(...
> [snip]
> p.parse(script, varargin{:});
>
> That's the example I'm following and I have used this
style in a lot
> of places in my code where it has seemingly worked correctly.
>
> What I took from the example is that required arguments
need to be
> named in the call to parse().
>
> 1. Is the example code I posted originally an incorrect
implementation
> of what is shown in Matlab help?
> 2. Am I misunderstanding the example?
> 3. Is the help example incorrect?
>
> Here's the help section I'm viewing:
> inputParser :: Functions (MATLAB Function Reference)
> help/techdoc/help.jar!/ref/inputparser.html
>
>



I could sure use some help on this. I am continuing to
struggle with the apparent differences in the help
documentation for the inputParser vs. what actually works. I
have not solved the issue I asked about in my original post
yet. The only suggestion (to not parse the required
arguments) doesn't seem satisfactory and it doesn't agree
with the help docs. What am I missing? Thanks.

Subject: inputParser not validating serial object correctly

From: per isakson

Date: 2 Oct, 2007 17:07:44

Message: 5 of 6

 "G.A.M." <x0Zero@gmail.com> wrote in message
<1190511764.536671.125930@y42g2000hsy.googlegroups.com>...
> My error msg is:
> Argument 'serialPort' failed validation @(sp)isa
(sp,'serial').
>
> My code is:
>
> function status = DoSomethingWithSerialPort(serialPort,
varargin)
>
> p = inputParser;
> p.addRequired('serialPort', @(sp)isa(sp, 'serial'));
> p.addParamValue('test','', @ischar);
> p.parse('serialPort', varargin{:});
> test= p.Results.test;
> %etc.
>
> After getting the error, I checked the class of the
function
> parameter, and it was as expected:
> class(serialPort)
> ans = serial
>
> Then, to continue debugging, I created this anonymous
function in the
> workspace:
> x = @(sp)isa(sp, 'serial')
>
> I used it to check the input parameter and it was again
as expected:
> x(serialPort)
> ans = 1
>
> So does anyone have any idea why I get the error shown at
the top?
> Thanks.
>

The example, publish_ip, in the documentation uses the same
name ("script") for both the name of the required variable
and the variable itself, which is asking for mistakes.

In this line

p.parse( serialPort, varargin{:});

<serialPort> is the name of the input variable. You have
two blips too many.

/ per
 

Subject: inputParser not validating serial object correctly

From: "G.A.M.

Date: 3 Oct, 2007 03:02:04

Message: 6 of 6

On Oct 2, 1:07 pm, "per isakson" <poi.nos...@bimDOTkthDOT.se> wrote:
> "G.A.M." <x0Z...@gmail.com> wrote in message
>
> <1190511764.536671.125...@y42g2000hsy.googlegroups.com>...
>
>
>
> > My error msg is:
> > Argument 'serialPort' failed validation @(sp)isa
> (sp,'serial').
>
> > My code is:
>
> > function status = DoSomethingWithSerialPort(serialPort,
> varargin)
>
> > p = inputParser;
> > p.addRequired('serialPort', @(sp)isa(sp, 'serial'));
> > p.addParamValue('test','', @ischar);
> > p.parse('serialPort', varargin{:});
> > test= p.Results.test;
> > %etc.
>
> > After getting the error, I checked the class of the
> function
> > parameter, and it was as expected:
> > class(serialPort)
> > ans = serial
>
> > Then, to continue debugging, I created this anonymous
> function in the
> > workspace:
> > x = @(sp)isa(sp, 'serial')
>
> > I used it to check the input parameter and it was again
> as expected:
> > x(serialPort)
> > ans = 1
>
> > So does anyone have any idea why I get the error shown at
> the top?
> > Thanks.
>
> The example, publish_ip, in the documentation uses the same
> name ("script") for both the name of the required variable
> and the variable itself, which is asking for mistakes.
>
> In this line
>
> p.parse( serialPort, varargin{:});
>
> <serialPort> is the name of the input variable. You have
> two blips too many.
>
> / per

Thank you very much. I have looked at this example many times and I
never noticed the fact that I had the extra blips.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
inputparser G.A.M. 2 Oct, 2007 11:00:12
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com