MATLAB - Timetables



Timetable is data structure available in matlab. It is nothing but tables with rows and columns with each row being timestamped. The data is stored in column-oriented data variables that will have different data types and sizes, but the same number of rows. Timetables provide a variety of time related functions. They deal with align, combine and also allows to do maths on the timestamped data in the given timetables.

Since the data stored are time-stamped , it helps to analyse data and can be useful in different domains and applications.In the real world there is a lot of usage where timetables can be brought into use. Some of the main areas where you can make use of timetables are as follows −

  • Medicinal Research − Here you can make use of timetables to store and analyse your data which are related to experiments on drugs and diseases. The experiments can be related to cardio respiratory, stem cells, infection and immunity etc. The data later can be plotted using functions like plot and histogram.
  • Stock Market Analysis − The data that comes from the stock market is huge.You can make use of Timetable to store and analyze it. There are other useful methods in matlab that can be used to calculate the numbers.
  • Environment Monitoring and Assessment − The data that comes down from environmental studies can be stored in timetables to do further analysis.
  • Sensor Data − With Timetables you can analyse and store sensor data related to GPS, temperature etc.

Let us understand in depth the working of timetables in matlab.

Creation of TimeTable

Matlab has many ways to create a timetable , here is a list of it.

  • Using timetable() function
  • Make use of array2timetable() function
  • Make use of table2timetable() function
  • Make use of timeseries2timetable() function

Using timetable() Function

Following is the syntax for timetable() function

Syntax

T = timetable(rowTimes,var1,...,varN)

The function timetable() will create a table where var1varN are input data variables. The parameter rowTimes is a time vector. In the above function the data types of the data variables var1..varN can vary but it should have the same number of rows. Same goes to rowTimes which is a vector with datetime or duration.

To create rowTimes will make use of the dateTime() function as shown below −

timer = datetime({'2023-01-01 09:00:00';'2023-01-01 09:30:00';'2023-01-01 10:00:00'});

When you execute the above in Matlab the output is as follows −

>> timer = datetime({'2023-01-01 09:00:00';'2023-01-01 09:30:00';'2023-01-01 10:00:00'})

timer = 

  31 datetime array

   01-Jan-2023 09:00:00
   01-Jan-2023 09:30:00
   01-Jan-2023 10:00:00

>>

Let us feed the timer and data variables to the timetable function and see the output in Matlab.

T = timetable(timer ,DayTemp ,DayPressure)

On execution in matlab we get the following output

>> timer = datetime({'2023-01-01 09:00:00';'2023-01-01 09:30:00';'2023-01-01 10:00:00'});
DayTemp = [28.3;30.0;33.3];
DayPressure = [23.1;23.03;33.9];
T = timetable(timer ,DayTemp ,DayPressure)

T =

  32 timetable

           timer            DayTemp    DayPressure
    ____________________    _______    ___________

    01-Jan-2023 09:00:00     28.3          23.1   
    01-Jan-2023 09:30:00       30         23.03   
    01-Jan-2023 10:00:00     33.3          33.9   

>>

Make use of array2timetable() Function

Following is the syntax for array2timetable() function

Syntax

The function array2timetable() will return a timetable wherein X is an MXN array and rowTimes is a vector array of size Mx1.

Let us see an example to create a timetable using array2timetable() function.

Example

Let us first create a 3x3 matrix as X.

X = rand(3,3)

Now let us create a row vector rowTimes.

rowTimes = seconds(1:3)

Now let us use X and rowTimes in array2timetable() function and see the output

X = rand(3,3)
rowTimes  = seconds(1:3)
TT = array2timetable(X,'RowTimes',rowTimes)

The output when executed in Matlab is as follows −

>> X = rand(3,3)
rowTimes  = seconds(1:3)
TT = array2timetable(X,'RowTimes',rowTimes)

X =

    0.6787    0.3922    0.7060
    0.7577    0.6555    0.0318
    0.7431    0.1712    0.2769


rowTimes = 

  13 duration array

   1 sec   2 sec   3 sec


TT =

  33 timetable

    Time       X1         X2          X3   
    _____    _______    _______    ________

    1 sec    0.67874    0.39223     0.70605
    2 sec    0.75774    0.65548    0.031833
    3 sec    0.74313    0.17119     0.27692

>>

Make use of table2timetable() Function

In this we are going to use a table to create a timetable out of it using the function table2timetable().

A table is a data type in Matlab, which stores data in tabular format, just the way you see data in a spreadsheet.

An example of table is as follows −

Name = {'Siya';'Riya';'Helen';'Reena'};
Age = [25;30;35;40];
Height = [149;150;160;153];
Weight = [50;65;48;52];
Timer = datetime({'2023-01-01 09:00:00';'2023-01-01 09:30:00';'2023-01-01 10:00:00';'2023-01-01 11:00:00'})
T = table(Name,Age,Height,Weight,Timer)

When you execute it in matlab the table is created as shown below −

T = table(Name,Age,Height,Weight, Timer)

T =

  45 table

      Name       Age    Height    Weight           Timer       
    _________    ___    ______    ______    ___________________

    {'Siya' }    25      149        50      2023-01-01 09:00:00
    {'Riya' }    30      150        65      2023-01-01 09:30:00
    {'Helen'}    35      160        48      2023-01-01 10:00:00
    {'Reena'}    40      153        52      2023-01-01 11:00:00

Let us now use the same table to create a timetable out of it.

The syntax of the function used is −

TT = table2timetable(T)

T is the table which is input to table2timetable() function. Let us execute the same in MAtlab

>> TT = table2timetable(T)

TT =

  44 timetable

           Timer              Name       Age    Height    Weight
    ____________________    _________    ___    ______    ______

    01-Jan-2023 09:00:00    {'Siya' }    25      149        50  
    01-Jan-2023 09:30:00    {'Riya' }    30      150        65  
    01-Jan-2023 10:00:00    {'Helen'}    35      160        48  
    01-Jan-2023 11:00:00    {'Reena'}    40      153        52  

>> 

Make use of timeseries2timetable() Function

The timeseries2timetable() function converts timeseries objects to timetables.

The syntax is as follows −

TT = timeseries2timetable(ts)

Here ts is the timeseries object.

The example below shows timeseries() that return timeseries object which we can later use inside timseries2timetable() function.

ts = timeseries(rand(5,1),[0 05 10 15 20])

The timeseries object is made up of 5 random number that are sampled at 5 second intervals.

The output when executed in matlab is −

>> ts = timeseries(rand(5,1),[0 05 10 15 20])
  timeseries

  Common Properties:
            Name: 'unnamed'
            Time: [5x1 double]
        TimeInfo: [1x1 tsdata.timemetadata]
            Data: [5x1 double]
        DataInfo: [1x1 tsdata.datametadata]

  More properties, Methods

>> ts.Time

ans =

     0
     5
    10
    15
    20

>> 

The property ts.Time returns the time.

Let us now use the timeseries object in timeseries2timetable() function

TT = timeseries2timetable(ts)

When you execute in matlab the output is as follows −

>> TT = timeseries2timetable(ts)

TT =

  51 timetable

     Time      Data  
    ______    _______

    0 sec     0.81472
    5 sec     0.90579
    10 sec    0.12699
    15 sec    0.91338
    20 sec    0.63236

>> 
Advertisements