Tables in MATLAB
Last Updated :
28 Nov, 2022
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:
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
name = { 'Harry' ; 'Mark' ; 'Steven' };
age = [23; 54; 13];
Employed = logical([1;0;1]);
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
name = { 'Harry' ; 'Mark' ; 'Steven' };
age = [23; 54; 13];
Employed = logical([1;0;1]);
emp = table(name,age,Employed);
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
name = { 'Harry' ; 'Mark' ; 'Steven' };
age = [23; 54; 13];
Employed = logical([1;0;1]);
emp = table(name,age,Employed);
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
name = { 'Harry' ; 'Mark' ; 'Steven' };
age = [23; 54; 13];
Employed = logical([1;0;1]);
emp = table(name,age,Employed);
emp{2:3,[2,3]}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...