Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the type of a variable in MATLAB

People also ask

How can you determine the type of a variable?

To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.

Which command is used to show the type of variable in MATLAB?

Check Type of Variable Using the whos Function in MATLAB If you want to check the type of every variable which is currently in the workspace of MATLAB, you can use the whos() function to check the variable's type. For example, let's store some value in a variable and then check its class.

Is there a type function in MATLAB?

The type function checks the directories specified in the MATLAB search path, which makes it convenient for listing the contents of M-files on the screen. Use type with more on to see the listing one screenful at a time.


Use the class function:

>> b = 2
b =
     2
>> a = 'Hi'
a =
Hi
>> class(b)
ans =
double
>> class(a)
ans =
char

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.


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 

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.


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: {0×1 cell}
        ContainingPackage: [0×0 meta.package]
     RestrictsSubclassing: 0
             PropertyList: [0×1 meta.property]
               MethodList: [272×1 meta.method]
                EventList: [0×1 meta.event]
    EnumerationMemberList: [0×1 meta.EnumeratedValue]
           SuperclassList: [0×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).