MATLAB Data Types

The basic data type (also called a class) in MATLAB is the array or matrix. There are 15 fundamental data types in MATLAB. Each of these data types is in the build of a matrix or array. This matrix or arrays are a minimum of 0-by-0 in size and can increase to an n-dimensional array of any size.

MATLAB Data Types

The following table describes these data types are:

Data TypeExampleDescription
int8, uint8,
int16, uint16,
int32, uint32,
int64, uint64
uint16(65000)The array of signed and unsigned integers. It requires less storage space than single or double. All integer data types except for int64 and uint64 can be used in mathematical operations.
single3 * 10^38The array of single-precision numbers. It requires less storage space than double but has less precision and a smaller range.
double3 * 10^300
5 + 6i
Array of double-precision numbers. Two-dimensional array can be sparse. The default numeric types in MATLAB.
logicalmagic(4) > 10Array of logical value of 1 or 0 to represent true and false respectively. Two-dimensional arrays can be sparse.
char'Hello'Array of characters. Strings are represented as vectors of characters. For arrays including more than one string, it is good to use cell arrays.
cell arraya{1,1} = 12;
a{1,2} = 'Red';
a{1,3} = magic(4);
Array of indexed cells, each capable of saving an array of a various dimension and data type.
structurea.day = 12;
a.color = 'Red';
a.mat = magic(3);
Array of C-like structures, each structure having named fields capable of storing an array of a different dimension and data type.
function handle@sinPointer to a function. You can pass function handles to other functions.
user classpolynom([0 -2 -5])Objects constructed from a user-defined class.
Java classjava.awt.FrameObjects constructed from a Java class.

Numeric Types

Numeric data types in MATLAB contain signed and unsigned integers, and single- and double-precision floating-point numbers. Integer and single-precision array offer more memory efficient storage than double-precision.

All numeric types provide basic array functions, such as subscripting and reshaping. All numeric types except for int64 and uint64 can be used in numerical operations.

Integers

MATLAB has four signed and four unsigned integers data type.

Signed types enable to work with a negative integer as well as positive, but cannot perform as wide a range of number as the unsigned type because one bit is used to designate positive or negative signs for the number.

Unsigned types give a wider range of numbers, but these numbers can only be zero or positive.

MATLAB provides 1-, 2-, 4-, and 8-byte storage for numeric data. We can save memory and execution time for our programs if we use the smallest integer type that accommodates your data. For example, we don't need a 32-bit integer to save the value 100.

Here are the eight numeric data type, the range of values can save with each type, and the MATLAB conversion operation required to create that type:

Data TypeRange of ValuesConversion Function
Signed 8-bit integer-27 to 27-1int8
Signed 16-bit integer-215 to 215-1int16
Signed 32-bit integer-231 to 231-1int32
Signed 64-bit integer-263 to 263-1int64
Unsigned 8-bit integer0 to 28-1uint8
Unsigned 16-bit integer0 to 216-1uint16
Unsigned 32-bit integer0 to 232-1uint32
Unsigned 64-bit integer0 to 264-1uint64

Creating Integer Data

MATLAB save numeric data as a double-precision floating-point by default. To save data as an integer, use one of the conversion functions shown in the table above:

We can use the whos function to show the dimensions, byte count, and data type of an array represented by a variable:

We can use the class function if we want to assign the output as shown here:

Use the isinteger function if you just want to verify that x is an integer:

Integer Functions

FunctionDescription
int8, int16,
int32, int64
It convert to signed 1-, 2-, 4-, or 8-byte integer.
uint8, uint16,
uint32, uint64
It converts to unsigned 1-, 2-, 4-, or 8-byte integer.
classIt returns the data type of an object.
isaIt determines if the input value has the specified data type.
isintegerIt determines if input value is an integer array.
isnumericIt determines if the input value is a numeric array.

Floating-Point Numbers

MATLAB show floating-point numbers in either double-precision or single-precision format. The default is double-precision, but we can make any number of single-precision with a simple conversion function.

Double-Precision Floating Point

MATLAB composes the double data type according to IEEE Standard 754 for double precision. Any value stored as a double-needed 64 bits, formatted as shown in the table below:

BitsUsage
63Sign (0 = positive, 1 = negative)
62 to 52Exponent, biased by 1023
51 to 0Fraction f of the number 1.f

Single-Precision Floating Point

MATLAB composes the single data type according to IEEE Standard 754 for single precision. Any value save as a single require 32 bits, formatted as shown in the table following:

BitsUsage
31Sign (0 = positive, 1 = negative)
30 to 23Exponent, biased by 127
22 to 0Fraction f of the number 1.f

Floating-Point Functions

FunctionDescription
doubleIt converts to double precision.
singleIt converts to single precision.
classIt returns the data type of an object.
isaIt determines if the input value has the specified data type.
isfloatIt determines if the input value is a floating-point array.
isnumericIt determines if the input value is a numeric array
epsIt returns the floating-point relative accuracy. This value is the tolerance MATLAB uses in its evaluation.
realmaxIt returns the largest floating-point number your computer can represent.
realminIt returns the smallest floating-point number our computer can represent.

Complex Numbers

Complex numbers consist of two separate part: a real part and an imaginary part. The primary imaginary unit is equal to the square root of -1. This is display in MATLAB by either of two letters: i or j.

Creating Complex Numbers

The following statement shows one method of creating a complex value in MATLAB. The variable x is assigned a complex number with a real part of 2 and an imaginary part of 3:

x = 2 + 3i;

Complex Number Functions

FunctionDescription
complexIt construct complex data from real and imaginary components.
i or jIt returns the imaginary unit used in constructing complex data.
realIt returns the real part of a complex number
imagIt returns the imaginary part of a complex number.
isrealIt determines if a number is real or imaginary.

Next TopicMATLAB Operator




Latest Courses