Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Syntax checking

Subject: Syntax checking

From: Adam

Date: 3 May, 2009 18:32:01

Message: 1 of 4

Hi all,

I am programming a toolbox and I have the following question:

Does matlab have any built in functions to confirm the correct syntax for a call to ones own function. For example, it would be nice to specify that the function needs either an array followed by a cell array, or, just the string 'all', or two strings where the first one is not 'all', etc...

If matlab had this, then one could easily put all syntax checking up front, and also expect to get nice error messages.

Thank you!
   Adam

Subject: Syntax checking

From: Matt Fig

Date: 3 May, 2009 20:23:02

Message: 2 of 4

The closest function that I know of is nargchk. But I don't think it fulfills all your requirements. I always write my own input checking which goes at the head of the function, it is not to hard to do this.

Subject: Syntax checking

From: Jiro Doke

Date: 3 May, 2009 20:49:01

Message: 3 of 4

"Adam " <king@cs.fairfield.edu> wrote in message <gtknv1$mr4$1@fred.mathworks.com>...
> Hi all,
>
> I am programming a toolbox and I have the following question:
>
> Does matlab have any built in functions to confirm the correct syntax for a call to ones own function. For example, it would be nice to specify that the function needs either an array followed by a cell array, or, just the string 'all', or two strings where the first one is not 'all', etc...
>
> If matlab had this, then one could easily put all syntax checking up front, and also expect to get nice error messages.
>
> Thank you!
> Adam

Adam,

If you're using R2007a or newer, you can use the inputParser class:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#bresuxt-11

(note that this is from the current documentation)

NARGCHK will check the number of arguments:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargchk.html

Also, VALIDATEATTRIBUTES and VALIDATESTRING may be useful (R2007b or newer):

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/validateattributes.html
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/validatestring.html


Hope this helps.

jiro

Subject: Syntax checking

From: Adam

Date: 3 May, 2009 21:28:01

Message: 4 of 4

Thank you both. This really helps a lot.

  -- Adam

Tags for this Thread

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.

Contact us