# MATLAB Check Variable Type

In this tutorial, we will discuss how we can check the type of a given variable using the `class()` and `whos` functions in MATLAB.

## Check Type of Variable Using the `class()` Function in MATLAB

In MATLAB, we don’t define a variable using its type. Instead, we store the required data in a variable. So, if at some point in the code we want to check the type of a variable, we can use the `class()` function to check the variable’s type. For example, let’s store some value in a variable and then check its class. See the below code.

``````clc
a = 123;
b = 'Hello';
type_of_a = class(a)
type_of_b = class(b)
``````

Output:

``````type_of_a =

double

type_of_b =

char
``````

In the above code, we have defined two variables, `a` and `b`, and then we are checking their type using the `class()` function. As you can see in the output, the type of the variable `a` is `double`, and the type of the variable `b` is `char`. You can check the type of any variable as long as it is defined or stored in the workspace.

## 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. See the below code.

``````clc
a = 123;
b = 'Hello';
whos
``````

Output:

``````  Name      Size            Bytes  Class     Attributes

a         1x1                 8  double
b         1x5                10  char
``````

The `whos` function will check the class of each variable, which is currently stored in the workspace. If you want to check the class or type of only one variable, then write the variable name after the `whos` function as shown below.

``````a = 123;
whos a
``````

In the above code, `whos` will only give you the class of the variable `a`.

## Check if the Variable is of the Specific Type or not in MATLAB

If you want to check a specific variable is of the required class or type, you can use that specific type’s function. For example, to check if a variable is of the type `char`, you can use the `ischar()` function, which will return `0` or `1` depending on the variable. If the variable is of the type `char`, the `ischar()` function will return `1`; otherwise, it will return `0`. See the example code below.

``````clc
a = 'Hello';
ischar(a)
``````

Output:

``````ans =

1
``````

In the above code, we checked if the variable `a` is of type `char` or not. As you can see in the output, the variable `a` is of type `char`. You can also use other type checking functions like the `iscell()` function to check if a variable is of type `cell` or not. You can use the `isinteger()` function to check if the variable is of type `int` or not. You can use the `isfloat()` function to check if the variable is of type `float` or not and many more functions. You can use these functions in conditions like the `if` statement. For example, let’s consider we want to check if a variable is of type char or not. If the variable is of type `char`, then we will display a message otherwise not.

``````clc
a = 'Hello World';
if ischar(a)
disp(a)
end
``````

Output:

``````Hello World
``````
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.