Open In App

MATLAB – Data Types

Last Updated : 04 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Matlab




% MATLAB code for variable initialization 
Geeks = 7;


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




% MATLAB code for random matrix generation
rng default
A = randi(5,5) % It will generate random matrix of 15x5
  
B = A < 9 % The result is a logical matrix.
% Each value in B represents a logical 1 (true) 
% or logical 0 (false) state to indicate whether
% the corresponding element of A fulfills the condition A < 9. 
% For example, A(1,1) is 13, so B(1,1) is logical 0 (false). 
% However, A(1,2) is 2, so B(1,2) is logical 1 (true).


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




% MATLAB code for showing String results
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




% MATLAB code for numeric type
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




% MATLAB code for Table 
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:

Matlab




C = {1, 2, 3}


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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads