MATLAB - Tables



In MATLAB, a table is a data structure that stores tabular type data of different types in named columns.You will be able to easily link tables with a spreadsheet, or database table. You can easily manage and manipulate data inside a Matlab table.

Since tables are meant to handle different data types, each column of the table can hold different types of data making it flexible for handling heterogenous type data.

In tables, individual sets of column-oriented data are stored within variables.Table variables can vary in terms of data types and sizes, as long as they all have the same number of rows. Just like how fields in a structure are named, table variables also carry specific names.

As we have worked a lot with matrices, let us understand how tables and matrices are different.

Matrices Tables

Matrices are meant to store the same type of data i.e all the elements will have the same data type. So matrices are homogeneous in nature.

The table columns can have different data types, making them suitable for handling mixed data.So tables are heterogeneous in nature.

In matrices, to access elements we make use of indexing for both rows and columns.

In tables, column names are used to access data within the table. This gives us a more descriptive and meaningful way to access the data.

Advantages of Using Tables

  • Table columns are assigned with names, so it makes it very easy to refer to and change specific data.

  • Table columns can have different data types, which allows you to store a variety of information in a single structure.

  • Table comes with built-in functions that help to give a quick overview of the data.It also helps to handle missing data and duplicates.

Disadvantages of Using Tables

  • Tables take up more memory compared to simple arrays for large datasets.

  • Some matrix operations might not be directly applicable to tables.

Creating Matlab Table

To create a matlab table we are going to make use of matlabs built-in table() function. You can use the table() function in two ways.

  • One is to create tables from existing workspace variables.

  • Second is to create a table that allows space for variables whose values are filled in later.

Syntax

T = table(var1,...,varN)
T = table('Size',sz,'VariableTypes',varTypes)

The first syntax table(var1.varN) ,here var1varN are variables that become a column in the table and they should have the same number of rows.The resulting table T will have these columns, with column names automatically assigned based on the variable names.

The second syntax table('Size',sz,'VariableTypes',varTypes),

'Size', sz − This specifies the size of the table, meaning the number of rows and columns. sz is a two-element vector where the first element represents the number of rows, and the second element represents the number of columns.

'VariableTypes', varTypes − This specifies the data types of the variables (columns) in the table. varTypes is a cell array where each cell contains the data type for a corresponding column.

T = table(var1,...,varN) − creates a table by combining existing variables var1 through varN, whereas

T = table('Size',sz,'VariableTypes',varTypes) − syntax pre allocates space for the table and defines variable types upfront. It's particularly useful when you know the size and types of data beforehand.

Examples on Table creation

Example 1

In this example let us check the student scores, so we have names of students and their scores.

std_names = {'Tiya', 'Riya', 'Siya', 'Jiya'}
std_scores = [85, 72, 92, 78]
studentTable = table(std_names, std_scores)

In the example above we have std_names and std_scores variables containing the students names and their scores. The table function combines these variables to create the studentTable, where the first column is named 'std_names' and the second column is named 'std_scores'.

On execution in matlab command window the output is −

std_names =
{
  [1,1] = Tiya
  [1,2] = Riya
  [1,3] = Siya
  [1,4] = Jiya
}

std_scores =

   85   72   92   78

Example 2

Using the same variables as used in the above examples let us add custom column names to it.

std_names = {'Tiya', 'Riya', 'Siya', 'Jiya'}
std_scores = [85, 72, 92, 78]
studentTable = table(std_names, std_scores, 'VariableNames', {'Student', 'TestScore'})

Now when you execute the same in the matlab command window, the table will have column names like Student and TestScore.

std_names =
{
  [1,1] = Tiya
  [1,2] = Riya
  [1,3] = Siya
  [1,4] = Jiya
}

std_scores =

   85   72   92   78

Example 3

In this example will create a Table with Size and Variable Types.

tableSize = [5, 3]
variableTypes = {'string', 'double', 'int32'}
Table = table('Size', tableSize, 'VariableTypes', variableTypes)

In this example, tableSize defines the size of the table as 5 rows and 3 columns, and variableTypes indicates that the first column should be of type 'string', the second column of type 'double', and the third column of type 'int32'.

When you execute the same in matlab command window the output is −

tableSize =

   5   3

variableTypes =
{
  [1,1] = string
  [1,2] = double
  [1,3] = int32
}

Accessing Table Data

Consider we have the following table.

std_names = {'Tiya', 'Riya', 'Siya', 'Jiya'}
std_scores = [85, 72, 92, 78]
studentTable = table(std_names, std_scores, 'VariableNames', {'Student', 'TestScore'})

Output will be −

std_names =
{
  [1,1] = Tiya
  [1,2] = Riya
  [1,3] = Siya
  [1,4] = Jiya
}

std_scores =

   85   72   92   78

To access data you can do so as follows −

T.Variables

For example

studentTable.Student
studentTable.TestScore

When you execute the above you get −

>> studentTable.Student
studentTable.TestScore


ans =

  1x4 cell array

    {'Tiya'}    {'Riya'}    {'Siya'}    {'Jiya'}


ans =

    85    72    92    78

To get the individual names and scores you can do the following −

studentTable.Student{1}

On execution you will get −

>> studentTable.Student{1}

ans =

    'Tiya'

Table with Row and Column Names

In the previous section we have seen how to add column names to your table.Here we are going to add row names and access the data from the table created.

Names = {'Siya';'Riya';'Jiya';'Tiya';'Kiya'};
Age = [48;23;28;20;49];
Height = [105;140;150;130;156];
Weight = [60;80;50;45;54];

T = table(Age,Weight,Height,'RowNames',Names)

The Age, Weight, and Height data vectors become the columns of the table.The 'RowNames' parameter assigns the names from the Names cell array as row names for each entry.

When you execute the same in matlab command window the output is −

>> Names = {'Siya';'Riya';'Jiya';'Tiya';'Kiya'};
Age = [48;23;28;20;49];
Height = [105;140;150;130;156];
Weight = [60;80;50;45;54];

T = table(Age,Weight,Height,'RowNames',Names)

T =

  5x3 table

            Age    Weight    Height
            ___    ______    ______

    Siya    48       60       105  
    Riya    23       80       140  
    Jiya    28       50       150  
    Tiya    20       45       130  
    Kiya    49       54       156  

Now when you want to access anything related to a name the same can be done as follows −

T('Riya',:)

On execution the output is −

>> T('Riya',:)

ans =

  1x3 table

             Age    Weight      Height
             ___    ______    ______

    Riya     23       80         140  

To fetch multiple rows you can do so as follows −

T({'Riya','Tiya'},:)

On execution the output is −

>> T({'Riya','Tiya'},:)

ans =

  2x3 table

            Age    Weight    Height
            ___    ______    ______

    Riya    23       80       140  
    Tiya    20       45       130  
Advertisements