tags:

views:

8101

answers:

4

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
+15  A: 

Use the class function

>> b = 2
b =
     2
>> a = 'Hi'
a =
Hi
>> class(b)
ans =
double
>> class(a)
ans =
char
Daniel LeCheminant
+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
+1  A: 

Another related function is WHOS. It will list all sorts of information (dimensions, byte size, type) for the variables in a given workspace.

gnovice

related questions