English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Data Types

MATLAB does not require any type declaration or dimension statement. Whenever MATLAB encounters a new variable name, it creates a variable and allocates appropriate memory space.

If a variable already exists, MATLAB will replace the original content with new content and allocate new storage space if necessary.

For example,

Total = 42

The following statement creates a named “Total”1than1matrix, and assign values42stored in it.

Data types available in MATLAB

MATLAB provides15types of basic data types. Each data type stores data in matrix or array form. The size of this matrix or array can be as small as 0×0, and can grow to any size of matrix or array.

The following table shows the most commonly used data types in MATLAB-

Serial numberData type and description
1

int8

8signed integer

2

uint8

8unsigned integer

3

int16

16signed integer

4

uint16

16unsigned integer

5

int32

32signed integer

6

uint32

32unsigned integer

7

int64

64signed integer

8

uint64

64unsigned integer

9

single

Single precision numerical data

10

double

Double precision numerical data

11

logical

Logical value1or 0, respectively representing true and false

12

char

Character data (strings stored as character vectors)

13

cell array

Index cell array, each cell can store arrays of different dimensions and data types

14

structure

Similar to C structures, each structure has named fields that can store arrays of different dimensions and data types

15

function handle

Pointer to a function

16

user classes

Objects constructed from user-defined classes

17

java classes

Objects constructed from Java classes

Instance

Create a script file using the following code-

str = 'Hello World!'
n = 2345
d = double(n)
un = uint32(789.50)
rn = 5678.92347
c = int32(rn)

After compiling and executing the above code, the following results will be produced-

str = Hello World!
n =  2345
d =  2345
un = 790
rn = 5678.9
c =  5679

Data type conversion

MATLAB provides various functions to convert values from one data type to another. The following table shows the data type conversion functions-

Function
Function
char

Convert to character array (string)

int2strConvert integer data to string
mat2str

Convert matrix to string

num2strConvert number to string
str2double

Convert string to double precision value

str2numConvert string to number
native2unicode

Convert numeric byte to Unicode character

unicode2nativeConvert Unicode character to numeric byte
base2dec

Convert N-ary number string to decimal number

bin2decConvert binary number string to decimal number
dec2base

Convert decimal number to N-ary number in a string

dec2binConvert decimal to binary number in a string
dec2hex

Convert decimal numbers in a string to hexadecimal numbers

hex2decConvert hexadecimal number string to decimal number
hex2numConvert hexadecimal number string to double precision number
num2hex

Convert single and double precision to IEEE hexadecimal string

cell2matConvert cell array to numeric array
cell2struct

Convert cell array to structure array

cellstrCreate a cell array of strings from a character array
mat2cellConvert array to cell array with possibly different cell sizes
num2cell

Convert array to cell array of uniform size

struct2cellConvert structure to cell array

Determine data type

MATLAB provides various functions to identify the data type of variables.

The following table provides the function to determine the data type of variables-

Function
Function
isDetect status
isaDetermine if the input is an object of a specified class
iscellDetermine if the input is a cell array
iscellstrDetermine if the input is a cell array of strings
ischarDetermine if the item is a character array
isfieldDetermine if the input is a field of a structure array
isfloatDetermine if the input is a floating-point array
ishghandle

Used for handling graphic object handles

isintegerDetermine if the input is an integer array
isjavaDetermine if the input is a Java object
islogicalDetermine if the input is a logical array
isnumericDetermine if the input is a numeric array
isobjectDetermine if the input is a MATLAB object
isrealCheck if the input is a real array
isscalarDetermine if the input is a scalar
isstrDetermine if the input is a character array
isstructDetermine if the input is a structure array
isvectorDetermine if the input is a vector
classDetermine the class of the object
validateattributesCheck the validity of the array
whosList variables in the workspace, including size and type

Instance

Create a script file using the following code-

x = 3
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
 
x = 23.54
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)
 
x = [1 2 3]
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
 
x = 'Hello'
isinteger(x)
isfloat(x)
isvector(x)
isscalar(x)
isnumeric(x)

When running the file, it will produce the following results-

x = 3
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
x = 23.540
ans = 0
ans = 1
ans = 1
ans = 1
ans = 1
x =
          1          2          3
ans = 0
ans = 1
ans = 1
ans = 0
x = Hello
ans = 0
ans = 0
ans = 1
ans = 0
ans = 0