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:
Capture function input and determine input variable type

Subject: Capture function input and determine input variable type

From: Angelia

Date: 27 Aug, 2013 13:29:08

Message: 1 of 4

I want my function myFun (num, string) to allow user to input either argument num or string (or both). After using nargin to determine the number of input, how do I capture the input and determine whether it's a number or a string?

Subject: Capture function input and determine input variable type

From: Steven_Lord

Date: 27 Aug, 2013 14:03:36

Message: 2 of 4



"Angelia " <tianqiuhua@gmail.com> wrote in message
news:kvi9j4$esg$1@newscl01ah.mathworks.com...
> I want my function myFun (num, string) to allow user to input either
> argument num or string (or both). After using nargin to determine the
> number of input, how do I capture the input and determine whether it's a
> number or a string?

So you want the signature of your function to be:

function y = myFun(x)

and you only want to allow x to be a number or a string? Or are you trying
to make the signature:

function y = myFun(in1, in2)

and accept myFun being called in any of these three ways:

myFun(1)
myFun('2')
myFun(3, '4')
myFun('5', 6)

Are these also allowed?

myFun()
myFun(7, 8)
myFun('9', '0')

You'll want to use CLASS and/or ISA to check the classes of the input data
types. CLASS lets you be very specific (I want to accept double precision
values but not single precision values or instances of a subclass of the
double class, for example) while ISA has some flexibility (I don't care what
type it is, as long as it's 'numeric' and I can use it as a number later in
my code.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Capture function input and determine input variable type

From: Angelia

Date: 27 Aug, 2013 20:38:08

Message: 3 of 4

"Steven_Lord" <slord@mathworks.com> wrote in message <kvibjo$k7p$1@newscl01ah.mathworks.com>...
>
>
> "Angelia " <tianqiuhua@gmail.com> wrote in message
> news:kvi9j4$esg$1@newscl01ah.mathworks.com...
> > I want my function myFun (num, string) to allow user to input either
> > argument num or string (or both). After using nargin to determine the
> > number of input, how do I capture the input and determine whether it's a
> > number or a string?
>
> So you want the signature of your function to be:
>
> function y = myFun(x)
>
> and you only want to allow x to be a number or a string? Or are you trying
> to make the signature:
>
> function y = myFun(in1, in2)
>
> and accept myFun being called in any of these three ways:
>
> myFun(1)
> myFun('2')
> myFun(3, '4')
> myFun('5', 6)
>
> Are these also allowed?
>
> myFun()
> myFun(7, 8)
> myFun('9', '0')
>
> You'll want to use CLASS and/or ISA to check the classes of the input data
> types. CLASS lets you be very specific (I want to accept double precision
> values but not single precision values or instances of a subclass of the
> double class, for example) while ISA has some flexibility (I don't care what
> type it is, as long as it's 'numeric' and I can use it as a number later in
> my code.)
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I allow myFun to be called in only these ways
myFun() (This one I can do by checking nargin)
myFun(1)
myFun('2')
myFun(3, '4')
But I can't distinguish between the two cases with one single input.
Hmm how should I use class?

Subject: Capture function input and determine input variable type

From: Steven_Lord

Date: 28 Aug, 2013 14:09:52

Message: 4 of 4



"Angelia " <tianqiuhua@gmail.com> wrote in message
news:kvj2ng$drd$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kvibjo$k7p$1@newscl01ah.mathworks.com>...
>>
>>
>> "Angelia " <tianqiuhua@gmail.com> wrote in message
>> news:kvi9j4$esg$1@newscl01ah.mathworks.com...

*snip*

> I allow myFun to be called in only these ways
> myFun() (This one I can do by checking nargin)
> myFun(1)
> myFun('2')
> myFun(3, '4')
> But I can't distinguish between the two cases with one single input.
> Hmm how should I use class?

What do these lines of code return?

classOfNumber = class(1)
classOfString = class('2')

Can you think of a way to use this information (perhaps with ISEQUAL or
SWITCH) to validate your inputs?

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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