The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Capture function input and determine input variable type
Replies: 3   Last Post: Aug 28, 2013 10:09 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: Capture function input and determine input variable type
Posted: Aug 27, 2013 10:03 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Angelia " <> wrote in message
> 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(3, '4')
myFun('5', 6)

Are these also allowed?

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
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.