199

Does MATLAB have a function/operator that indicates the type of a variable (similar to the typeof operator in JavaScript)?

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
asked Feb 23, 2009 at 17:27
0

6 Answers 6

236

Use the class function:

>> b = 2
b =
 2
>> a = 'Hi'
a =
Hi
>> class(b)
ans =
double
>> class(a)
ans =
char
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Feb 23, 2009 at 17:36
Sign up to request clarification or add additional context in comments.

2 Comments

for conditionals: if ( string(class(b)) == 'double' ) fprintf(1, 'b is double'); end
Do note that you can overload the class method in a custom class to return any random string.
71

The 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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Feb 23, 2009 at 17:33

1 Comment

Do note that in a custom class you can overload all of these methods to return anything you want.
46

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

>> a = [0 0 7];
>> whos a
 Name Size Bytes Class Attributes
 a 1x3 24 double 
>> b = 'James Bond';
>> whos b
 Name Size Bytes Class Attributes
 b 1x10 20 char 
answered Mar 11, 2009 at 21:11

1 Comment

The WHO function does not list the size of variables. Because of that it is much faster if your workspace is crowded.
28

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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Mar 11, 2009 at 20:57

1 Comment

Is this a response to Dima's answer?
5

Since nobody mentioned it, MATLAB also has the metaclass function, which returns an object with various bits of information about the passed-in entity. These meta.class objects can be useful for tests of inheritance (via common comparison operators).

For example:

>> metaclass(magic(1))
ans = 
 class with properties:
 Name: 'double'
 Description: ''
 DetailedDescription: ''
 Hidden: 0
 Sealed: 0
 Abstract: 0
 Enumeration: 0
 ConstructOnLoad: 0
 HandleCompatible: 0
 InferiorClasses: ×ばつ1 cell}
 ContainingPackage: ×ばつ0 meta.package]
 RestrictsSubclassing: 0
 PropertyList: ×ばつ1 meta.property]
 MethodList: ×ばつ1 meta.method]
 EventList: ×ばつ1 meta.event]
 EnumerationMemberList: ×ばつ1 meta.EnumeratedValue]
 SuperclassList: ×ばつ1 meta.class]
>> ?containers.Map <= ?handle
ans =
 logical
 1

We can see that class(someObj) is equivalent to the Name field of the result of metaclass(someObj).

answered Oct 23, 2017 at 10:38

Comments

0

MATLAB - Checking type of variables

class() exactly works like Javascript's typeof operator.

To get more details about variables you can use whos command or whos() function.

Here is the example code executed on MATLAB R2017a's Command Window.

>> % Define a number
>> num = 67
num =
 67
>> % Get type of variable num
>> class(num)
ans =
 'double'
>> % Define character vector
>> myName = 'Rishikesh Agrawani'
myName =
 'Rishikesh Agrwani'
>> % Check type of myName
>> class(myName)
ans =
 'char'
>> % Define a cell array
>> cellArr = {'This ', 'is ', 'a ', 'big chance to learn ', 'MATLAB.'}; % Cell array
>> 
>> class(cellArr)
ans =
 'cell'
>> % Get more details including type
>> whos num
 Name Size Bytes Class Attributes
 num 1x1 8 double 
>> whos myName
 Name Size Bytes Class Attributes
 myName 1x17 34 char 
>> whos cellArr
 Name Size Bytes Class Attributes
 cellArr 1x5 634 cell 
>> % Another way to use whos i.e using whos(char_vector)
>> whos('cellArr')
 Name Size Bytes Class Attributes
 cellArr 1x5 634 cell 
>> whos('num')
 Name Size Bytes Class Attributes
 num 1x1 8 double 
>> whos('myName')
 Name Size Bytes Class Attributes
 myName 1x17 34 char 
>> 
answered May 10, 2018 at 9:04

1 Comment

This doesn't add anything to the currently accepted answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.