MATLAB Data types

What is Data Type?

A data type is a classification that helps to decide what class of value a variable can reserve in programming. And also, what sort of operations can be done on that variable without altering the value it stores.

MATLAB’s Data Types

In MATLAB, all numeric variables are stored as double-precision floating-point. Data types in MATLAB allow stores text, images, single-precision values in a single variable.

Matrix or Array  is the primary data type in MATLAB. MATLAB is loosely typed or dynamically typed, meaning it does not require any type declaration at the time of compilation. At runtime, wherever a variable is encountered, memory is allocated dynamically.

Numeric data types

These numeric classes include signed integers, unsigned integers, precision, double-precision floating-point numbers. All numeric values are stored as double-precision floating-point by default. Basic operations like indexing, mathematical operations, reshaping are supported by all numeric type. We will discuss this data type later in this tutorial.

Characters and strings

MATLAB’s characters and strings supports all kind of text data. It basically stores small piece of textual data, which can be used further need.

Str represents string data type whereas c represents character data type.

Dates and Time

This data types provides us with the current date, current time or to schedule time, to calculate duration of a particular task. We can perform operations, such as concatenate, sort, add, subtract and plot date and time values.

Categorical Arrays

Values from a finite set of discrete categories are stored by a data type called categorical.

Tables

Tabular data or column-oriented data is represented by table data type.

In a table it is not necessary to have same data type variables and can have different size but with one simple restriction that each must have same number of rows.

Timetables

This data type is used for time-stamped data to represent in tabular form. To associate time with each row timetable type of table is used. It helps to combine, align, perform calculations with timetables and also to synchronize data in multiple timetables.

Structures

Toassemble logically related data using data containers called fieldsstructure data type is used.

Any type of data can be stored in each of these fields. By using dot notation, we can access data in a structure. Just like this structName.fieldName.

Cell Arrays

A cell is a indexed data container that stores data implemented through cell arrays. Each cell can contain records of charactervectors, combination of strings and characters, records of numeric arrays.

Function handles

This MATLAB’s data type represents a function. Function handles are usedto pass a function as an input to another function.

`Example: h=@(x, y) (x.^3 -y.^3);`

This creates a handle to evaluate a function for the expression x3-y3. We have also shown in the below example script code.

Map containers

To associate each value of Map object data structure with a corresponding key in order to store value we use this data type called Map Containers. Few functions like isKey(), remove(), values(), containers.Map(), keys() are used to provide more flexibility to access data from Map containers.

Time series

Time-evolution of a dynamic process is represented by Time series data type. In this we can store time-stamped data as column data variables. In this data type we can store data vectors sampled over discrete time intervals.

Example:

We have created a script file, which contains few of the above-mentioned data types examples.

``` %we are declaring a string to variable string1
string1 = "welcome to the world"
%we are creating a character, difference between a char and string is only
%the double quotation and single quotation
C = 'welcome to the world'
% we are creating a categorical array as follows
stations = {'p1';'p2';'p1';'p3';'p2'};
Stations = categorical(stations)
% we are creating a table as follows
T1 = table({'M';'M';'F';'F';'F'},[88;93;78;30;69], ...
[91;79;44;97;94],[126;613;931;933;229])
% we are creating field with some values assigned and displayed it as structure data type as follows
field1 = 'f';
value = {'some text';
[70, 30, 80];
magic(5)};
s = struct(field1,value)
%For example, create a handle to an anonymous function that evaluates the expression p2 ? z2:
f = @(z,p) (p.^4 - z.^4);
%Create a Map object that contains rainfall data for several months. The map contains the four values in valueSet, and the keys are the four month names in keySet
keyset1 = {'J','F','M','A'};
valueset1 = [27.2 68.2 97.6 78.9];
m1 = containers.Map(keyset1,valueset1)
%Create a timeseries object with 5 scalar data samples, specifying a name for the timeseries.
ts1 = timeseries((1:5)','Name','MyTimeSeries'); ```

If we run the above script file in MATLAB’s environment, we will get this expected output.

``` string1 =
"welcome to the world"
C =
'welcome to the world'
Stations =
5×1 categorical array
p1
p2
p1
p3
p2
T1 =
5×4 table
Var1     Var2    Var3    Var4
_____    ____    ____    ____
{'M'}     88      91     126
{'M'}     93      79     613
{'F'}     78      44     931
{'F'}     30      97     933
{'F'}     69      94     229
s =
3×1 struct array with fields:
f
m1 =
Map with properties:
Count: 4
KeyType: char
ValueType: double ```

Fundamental data types

There are 15 basic fundamental data types in MATLAB as follows.

Example :

The below code can be created in one script file and executed one by one in the command area. The workspace is highlighted in the below figure showing how the variables created are stored with the names of respective datatypes.

``` a=65
b=”welcome”
c=false
d=56.78
g=uint32(789.50)
h=’c’ ```

Output:

Data Type Conversion

Data type conversion means converting a variable data type to another data type. There are two types of data type conversion: implicit conversion, which is automatic type conversion, and explicit conversion, which is done manually by the programmer. MATLAB provides data type conversion functions to make this job easy.

Example:

We have created a script file, which contains few of the above-mentioned functions.

``` %Convert the array to character
A = [77 65 84 76 65 66];
C = char(A)
%Convert the array to string
B = ["Past","Present","Future"]
D = cellstr(B)
%Convert the value int to string
chr = int2str(256)
%Convert the value matrix to string
chr1 = mat2str([3.85 2.91; 7.74 8.99])
%Convert the value number to string
S = num2str(pi)
%Convert the value string to double
X = str2num('100')
%Convert the value string to double
Y = str2double('3.1416')
%Convert the value base to decimal
baseStr = '1B';
D1 = base2dec(baseStr,12)
%Convert the value to a decimal to base number.
D2 = 23;
baseStr = dec2base(D2,12)
%Convert a character vector that represents a hexadecimal value to a decimal number.
hexStr = '3FF';
D3 = hex2dec(hexStr)
If we run the above code, we will get this expected output.
C =
'MATLAB'
B =
1×3 string array
"Past"    "Present"    "Future"
D =
1×3 cell array
{'Past'}    {'Present'}    {'Future'}
chr =
'256'
chr1 =
'[3.85 2.91;7.74 8.99]'
S =
'3.1416'
X =
100
Y =
3.1416
D1 =
23
baseStr =
'1B'
D3 =
1023
30    52   14 ```

Data type determination

There are few functions available in MATLAB to determine the datatype of a given variable.

Let us list out few of these functions.

Let us understand through example code.

We have created a script file, which contains few of the above-mentioned functions.

``` q=98
isinteger(q)
isfloat(q)
isvector(q)
isnumeric(q)
p=56.78
isinteger(p)
isfloat(p)
isvector(p)
isnumeric(p)
s=[1 2 3]
isinteger(s)
isfloat(s)
isvector(s)
isscalar(s)
t="welcome"
isinteger(q)
isfloat(q)
isvector(q)
isnumeric(q)

```

If we run the above code, we will get this expected output.

```q=98
ans=0
ans=1
ans=1
ans=1
p=56.78
ans=0
ans=1
ans=1
ans=1
s=[1 2 3]
ans=0
ans=1
ans=1
ans=0
t=welcome
ans=0
ans=0
ans=1
ans=0 ```

Conclusion:

In the above explanation, the different data types of MATLAB programming language are simplified and are useful for everyone,to perform scientific sort of compose effective MATLAB programs that are quick, streamlined for execution, and versatile for future necessities.