Open In App

Tables in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Table is an array data type in MATLAB that stores column-based or tabular data of same or different types. A table stores each column-oriented data under a variable name (column name). These table columns can have different data types in each however, the number of data points in every column must be the same.

Creating Tables:

Creating Empty or 0 by 0 Table:

This can be done by a simple command.

Example 1:

Matlab




% MATLAB Code for create table
t = table();


This creates an empty table named t.

Creating Table Using Arrays:

One can create tables from arrays of different data types but, same size.

Example 2:

Matlab




% MATLAB Code for table creation
name = {'Harry';'Mark';'Steven'};
age = [23; 54; 13];
Employed = logical([1;0;1]);
 
% Creating table with above data
emp = table(name,age,Employed)


Output:

 

Accessing Table Elements:

By Smooth Parenthesis ():

This method returns requested columns and table variables. 

Syntax:

Table (<number of rows>, <variable list>)

Let us index the first two rows and the first and third variables of the above table.

Example 3: 

Matlab




% MATLAB Code
name = {'Harry';'Mark';'Steven'};
age = [23; 54; 13];
Employed = logical([1;0;1]);
emp = table(name,age,Employed);
 
% Accessing the said rows and columns
emp(1:2,[1,3])


Output :

 

This extracted first 2 rows from first and third variable.

By Dot Notation:

The dot notation returns the passed variable as an array. 

Example 4: 

Matlab




% MATLAB Code for dot notation
name = {'Harry';'Mark';'Steven'};
age = [23; 54; 13];
Employed = logical([1;0;1]);
emp = table(name,age,Employed);
 
% Accessing age of second and third row
emp.age(2:3)


Output:

 

By Curly Braces Notation {}:

This method returns a concatenated array in the range of given rows and table variables. 

Example 5: 

Matlab




% MATLAB  Code for curly braces notation
name = {'Harry';'Mark';'Steven'};
age = [23; 54; 13];
Employed = logical([1;0;1]);
emp = table(name,age,Employed);
 
% Accessing a sub table as an array
emp{2:3,[2,3]}


Output:

 



Last Updated : 28 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads