MATLAB Data TypesThe 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. The following table describes these data types are: Data Type | Example | Description |
---|
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. | single | 3 * 10^38 | The array of single-precision numbers. It requires less storage space than double but has less precision and a smaller range. | double | 3 * 10^300 5 + 6i | Array of double-precision numbers. Two-dimensional array can be sparse. The default numeric types in MATLAB. | logical | magic(4) > 10 | Array 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 array | a{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. | structure | a.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 | @sin | Pointer to a function. You can pass function handles to other functions. | user class | polynom([0 -2 -5]) | Objects constructed from a user-defined class. | Java class | java.awt.Frame | Objects constructed from a Java class. |
Numeric TypesNumeric 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. IntegersMATLAB 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 Type | Range of Values | Conversion Function |
---|
Signed 8-bit integer | -27 to 27-1 | int8 | Signed 16-bit integer | -215 to 215-1 | int16 | Signed 32-bit integer | -231 to 231-1 | int32 | Signed 64-bit integer | -263 to 263-1 | int64 | Unsigned 8-bit integer | 0 to 28-1 | uint8 | Unsigned 16-bit integer | 0 to 216-1 | uint16 | Unsigned 32-bit integer | 0 to 232-1 | uint32 | Unsigned 64-bit integer | 0 to 264-1 | uint64 |
Creating Integer DataMATLAB 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 FunctionsFunction | Description |
---|
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. | class | It returns the data type of an object. | isa | It determines if the input value has the specified data type. | isinteger | It determines if input value is an integer array. | isnumeric | It determines if the input value is a numeric array. |
Floating-Point NumbersMATLAB 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 PointMATLAB 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: Bits | Usage |
---|
63 | Sign (0 = positive, 1 = negative) |
---|
62 to 52 | Exponent, biased by 1023 |
---|
51 to 0 | Fraction f of the number 1.f |
---|
Single-Precision Floating PointMATLAB 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: Bits | Usage |
---|
31 | Sign (0 = positive, 1 = negative) | 30 to 23 | Exponent, biased by 127 | 22 to 0 | Fraction f of the number 1.f |
Floating-Point FunctionsFunction | Description |
---|
double | It converts to double precision. | single | It converts to single precision. | class | It returns the data type of an object. | isa | It determines if the input value has the specified data type. | isfloat | It determines if the input value is a floating-point array. | isnumeric | It determines if the input value is a numeric array | eps | It returns the floating-point relative accuracy. This value is the tolerance MATLAB uses in its evaluation. | realmax | It returns the largest floating-point number your computer can represent. | realmin | It returns the smallest floating-point number our computer can represent. |
Complex NumbersComplex 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 NumbersThe 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 FunctionsFunction | Description |
---|
complex | It construct complex data from real and imaginary components. | i or j | It returns the imaginary unit used in constructing complex data. | real | It returns the real part of a complex number | imag | It returns the imaginary part of a complex number. | isreal | It determines if a number is real or imaginary. |
|