MATLAB – Data Types
Last Updated :
04 Jul, 2021
MATLAB is a platform which provides millions of Engineers and Scientists to analyze data using programming and numerical computing algorithm and also help in creating models. Data types are particular types of data items defined by the values they can store in them, generally, in programming languages they are used.
Define data types in MATLAB
In MATLAB we do not require any type of declaration statement, when it gets any new variable name it creates the variable and allocates appropriate memory space to it but if the variable name already exists it will replace the original content with new content and allocate it to new storage space when required.
Syntax: variable name = a value (or an expression)
Example:
Output:
Data Types in MATLAB
In MATLAB data can be stored in different types, numeric, text, complex number, etc. To store these data MATLAB has different classes which have various characteristics. MATLAB provides a total of 16 fundamental data types.
Logic Type
Logic types are True and false values that are represented with the logical value 0 and 1. Any numerical value (non-complex) can be converted into a logical representation.
Syntax:G = logical (x)
Example:
Matlab
rng default
A = randi(5,5)
B = A < 9
|
Output:
Char and String type
In MATLAB character and string array provide storage for text type data. The strings are character array compared with the sequence of numbers called a numeric array.
Syntax: s = ‘String’
Example:
Matlab
str = "Welcome to GeeksforGeeks, "
"Welcome!" " and lets start coding."
fprintf(str);
|
Output:
Numeric Type-
Integer and floating-point data are in this type with the following descriptions.
Data Type |
Short Description |
Features |
double |
Double-precision arrays |
- Default numeric data type (class) in MATLAB
- Stored as 64-bit (8-byte) floating-point value
- Range:
Negative numbers = -1.79769 x 10308 to -2.22507 x 10-308
Positive numbers = 2.22507 x 10-308 to 1.79769 x 10308
|
single |
Single-precision arrays |
- Stored as 4-byte (32-bit) floating-point value
- Range-
Negative numbers = -1.79769 x 10308 to -2.22507 x 10-308
Positive numbers = 2.22507 x 10-308 to 1.79769 x 10308
|
int8 |
8-bit signed integer arrays |
- Stored as 1-byte (8-bit) signed integers
- Range is -27 to 27-1
|
int16 |
16-bit signed integer arrays |
- Stored as 2-byte (16-bit) signed integers
- Range -215 to 215 -1
|
int32 |
32-bit signed integer arrays |
- Stored as 4-byte (32-bit) signed integers
- Range is -231 to 231-1
|
int64 |
64-bit signed integer arrays |
- Stored as 8-byte (64-bit) signed integers
- Range is -263 to 263-1
|
uint8 |
8-bit unsigned integer arrays |
- Stored as 1-byte (8-bit) unsigned integers
- Range is 0 to 28-1
|
unit16 |
16-bit unsigned integer arrays |
- Stored as 2-byte (16-bit) unsigned integers
- Range is 0 to 216 -1
|
uint32 |
32-bit unsigned integer arrays |
- Stored as 4-byte (32-bit) unsigned integers
- Range is 0 to 232-1
|
uint64 |
64-bit unsigned integer arrays |
- Stored as 8-byte (64-bit) unsigned integers
- Range is 0 to 264-1
|
Example:
Matlab
str = 'Hello World' ;
int8(str)
|
Output:
Table
The table contains rows and column variables. Each variable can be of different data types and different sizes, but each variable needs to have the same number of rows. Range of functions are used to access data to create, edit, and read the table data.
Syntax:T = table(ColumnName1,ColumnName2);
Example:
Matlab
T = table(Name,QuestionAttempted,CodingScore);
data = { 'Atul Sisodiya' ,22,100};
Tnew = [Tnew;data];
|
Output:
Table array
2x3
Name QuestionAttempted CodingScore
Atul Sisodiya 22 100
Cell
A cell array is a MATLAB data type that contains indexed data containers called cells. Cells can contain any type of data, commonly contain character vectors of different lengths, numbers, an array of numbers of any size. Sets of cells are enclosed in () and access to the cells is done by using {} which is to create, edit or delete any cell functions.
Syntax: c = { }
Example:
Output:
Structure
In structure data containers are used to group related data and their type, which are called fields. Fields may contain any type of data. In structures, Data is accessed using the dot notation.
Syntax: structname.fieldName
Example:
Matlab
geek(1).name = ( "Atul Sisodiya" );
geek(1).coding = 100;
geek
|
Output:
Function Handles
Function Handles is majorly used in MATLAB is to pass a function (numerical or char) to another function. Variables that are used to invoke function indirectly can be named as a Function handle.
To create a function handle ‘@’ operator is used.
Example: To create a function handle to evaluate x^2 + y^2, a function used is:
Output:
Share your thoughts in the comments
Please Login to comment...