Does MATLAB have a function/operator that indicates the type of a variable (similar to the typeof operator in JavaScript)?
+7
A:
[heavy edits]
class()
function is the equivalent of typeof()
You can also use isa()
to check if a variable is of a particular type.
If you want to be even more specific, you can use ischar()
, isfloat()
, iscell()
, etc.
Dima
2009-02-23 17:33:41
+15
A:
Use the class
function
>> b = 2
b =
2
>> a = 'Hi'
a =
Hi
>> class(b)
ans =
double
>> class(a)
ans =
char
Daniel LeCheminant
2009-02-23 17:36:42
+3
A:
Be careful when using the isa
function. This will be true if your object is of the specified type or one of its subclasses. You have to use strcmp
with the class
function to test if the object is specifically that type and not a subclass.
Mike Katz
2009-03-11 20:57:01