There are 16 fundamental data types, or classes, in MATLAB. Each of these classes is in the form of a matrix or array. With the exception of function handles, this matrix or array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size. A function handle is always scalar (1-by-1).
Important moment in MATLAB is that you don't need to use any type declaration or dimension statements by default. When you define new variable MATLAB creates it automatically and allocates appropriate memory space.
Example:
a = 123;
b = [1 2 3];
c = '123';
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x3 24 double
c 1x3 6 char
If the variable already exists, MATLAB replaces the original data with new one and allocates new storage space if necessary.
Fundamental data types
Fundamental data types are: numeric, logical
, char
, cell
, struct
, table
and function_handle
.
Floating-Point numbers (default)
MATLAB represents floating-point numbers in either double-precision or single-precision format. The default is double precision, but you can make any number single precision with a simple conversion function:
a = 1.23;
b = single(a);
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 4 single
MATLAB has four signed and four unsigned integer classes. Signed types enable you to work with negative integers as well as positive, but cannot represent as wide a range of numbers as the unsigned types because one bit is used to designate a positive or negative sign for the number. Unsigned types give you a wider range of numbers, but these numbers can only be zero or positive.
MATLAB supports 1-, 2-, 4-, and 8-byte storage for integer data. You can save memory and execution time for your programs if you use the smallest integer type that accommodates your data. For example, you do not need a 32-bit integer to store the value 100.
a = int32(100);
b = int8(100);
>> whos
Name Size Bytes Class Attributes
a 1x1 4 int32
b 1x1 1 int8
To store data as an integer, you need to convert from double to the desired integer type. If the number being converted to an integer has a fractional part, MATLAB rounds to the nearest integer. If the fractional part is exactly 0.5
, then from the two equally nearby integers, MATLAB chooses the one for which the absolute value is larger in magnitude.
a = int16(456);
Character arrays provide storage for text data in MATLAB. In keeping with traditional programming terminology, an array (sequence) of characters is defined as a string. There is no explicit string type in retail releases of MATLAB.
logical: logical values of 1 or 0, represent true and false respectively. Use for relational conditions and array indexing. Because it's just TRUE or FALSE it has size of 1 byte.
a = logical(1);
structure. A structure array is a data type that groups variables of different data types using data containers called fields. Each field can contain any type of data. Access data in a structure using dot notation of the form structName.fieldName.
field1 = 'first';
field2 = 'second';
value1 = [1 2 3 4 5];
value2 = 'sometext';
s = struct(field1,value1,field2,value2);
In order to access value1, each of the following syntax are equivalent
s.first or s.(field1) or s.('first')
We can explicitly access a field we know will exist with the first method, or either pass a string or create a string to access the field in the second example. The third example is demostrating that the dot parenthases notation takes a string, which is the same one stored in the field1 variable.
table variables can be of different sizes and data types, but all variables must have the same number of rows.
Age = [15 25 54]';
Height = [176 190 165]';
Name = {'Mike', 'Pete', 'Steeve'}';
T = table(Name,Age, Height);
cell. It's very useful MATLAB data type: cell array is an array each element of it can be of different data type and size. It's very strong instrument for manipulating data as you wish.
a = { [1 2 3], 56, 'art'};
or
a = cell(3);
function handles stores a pointer to a function (for example, to anonymous function). It allows you to pass a function to another function, or call local functions from outside the main function.
There are a lot of instruments to work with each data type and also built-in data type conversion functions (str2double
, table2cell
).
Additional data types
There are several additional data types which are useful in some specific cases. They are:
Date and time: arrays to represent dates, time, and duration.
datetime('now')
returns 21-Jul-2016 16:30:16
.
Categorical arrays: it's data type for storing data with values from a set of discrete categories. Useful for storing nonnumeric data (memory effective). Can be used in a table to select groups of rows.
a = categorical({'a' 'b' 'c'});
Map containers is a data structure that has unique ability to indexing not only through the any scalar numeric values but character vector. Indices into the elements of a Map are called keys. These keys, along with the data values associated with them, are stored within the Map.
Time series are data vectors sampled over time, in order, often at regular intervals. It's useful to store the data connected with timesteps and it has a lot of useful methods to work with.