Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

table2timetable

Convert table to timetable

Syntax

TT = table2timetable(T)
TT = table2timetable(T,'RowTimes',timeVarName)
TT = table2timetable(T,'RowTimes',rowTimes)

Description

example

TT = table2timetable(T) converts the table T to a timetable. The first datetime or duration variable in T becomes the vector of row times of TT. The remaining variables of T become the variables of TT.

  • If T is an M-by-N table without row names, then TT is an M-by-(N-1) timetable.

  • If T is an M-by-N table with row names, then table2timetable assigns the row names of T to a variable of TT. As a result, TT is an M-by-N timetable.

For more information on creating and using timetables, see Timetables.

To create a timetable from data in a text or spreadsheet file, first use the readtable function to read the data to a table. Then convert the table to a timetable with table2timetable.

example

TT = table2timetable(T,'RowTimes',timeVarName) converts T to a timetable. timeVarName is the name of a variable in T that contains datetime or duration values, and that table2timetable assigns as the vector of row times of TT. timeVarName can be the name of any variable in T that contains datetime or duration values.

example

TT = table2timetable(T,'RowTimes',rowTimes) converts T to a timetable. rowTimes is an M-by-1 vector of datetime or duration values. All of the variables of T become variables of TT.

Examples

collapse all

Convert a table that contains dates and times to a timetable.

Read power outage data from the file outages.csv to a table. The table contains both outage and restoration times.

T = readtable('outages.csv');
T(1:5,:)
ans=5x6 table
      Region          OutageTime        Loss     Customers     RestorationTime           Cause      
    ___________    ________________    ______    __________    ________________    _________________

    'SouthWest'    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    'winter storm'   
    'SouthEast'    2003-01-23 00:49    530.14    2.1204e+05    NaT                 'winter storm'   
    'SouthEast'    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    'winter storm'   
    'West'         2004-04-06 05:44    434.81    3.4037e+05    2004-04-06 06:10    'equipment fault'
    'MidWest'      2002-03-16 06:18    186.44    2.1275e+05    2002-03-18 23:23    'severe storm'   

Convert the table to a timetable. The first variable with times, OutageTime, becomes the time vector of TT.

TT = table2timetable(T);
TT(1:5,:)
ans=5x5 timetable
       OutageTime         Region        Loss     Customers     RestorationTime           Cause      
    ________________    ___________    ______    __________    ________________    _________________

    2002-02-01 12:18    'SouthWest'    458.98    1.8202e+06    2002-02-07 16:50    'winter storm'   
    2003-01-23 00:49    'SouthEast'    530.14    2.1204e+05    NaT                 'winter storm'   
    2003-02-07 21:15    'SouthEast'     289.4    1.4294e+05    2003-02-17 08:14    'winter storm'   
    2004-04-06 05:44    'West'         434.81    3.4037e+05    2004-04-06 06:10    'equipment fault'
    2002-03-16 06:18    'MidWest'      186.44    2.1275e+05    2002-03-18 23:23    'severe storm'   

Index into TT using row times from its time vector. You can treat the row times as labels that specify rows.

TT('2003-02-07 21:15',:)
ans=1x5 timetable
       OutageTime         Region       Loss     Customers     RestorationTime         Cause     
    ________________    ___________    _____    __________    ________________    ______________

    2003-02-07 21:15    'SouthEast'    289.4    1.4294e+05    2003-02-17 08:14    'winter storm'

Calculate the duration of power outages. Use dot syntax to extract the row times as a vector.

TT.OutageDuration = TT.RestorationTime - TT.OutageTime;
TT(1:5,:)
ans=5x6 timetable
       OutageTime         Region        Loss     Customers     RestorationTime           Cause          OutageDuration
    ________________    ___________    ______    __________    ________________    _________________    ______________

    2002-02-01 12:18    'SouthWest'    458.98    1.8202e+06    2002-02-07 16:50    'winter storm'       148:32:00     
    2003-01-23 00:49    'SouthEast'    530.14    2.1204e+05    NaT                 'winter storm'             NaN     
    2003-02-07 21:15    'SouthEast'     289.4    1.4294e+05    2003-02-17 08:14    'winter storm'       226:59:00     
    2004-04-06 05:44    'West'         434.81    3.4037e+05    2004-04-06 06:10    'equipment fault'     00:26:00     
    2002-03-16 06:18    'MidWest'      186.44    2.1275e+05    2002-03-18 23:23    'severe storm'        65:05:00     

Convert a table to a timetable and specify the table variable that becomes the time vector of the timetable.

Read power outage data from the file outages.csv to a table. The table contains both outage and restoration times.

T = readtable('outages.csv');
T(1:5,:)
ans=5x6 table
      Region          OutageTime        Loss     Customers     RestorationTime           Cause      
    ___________    ________________    ______    __________    ________________    _________________

    'SouthWest'    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    'winter storm'   
    'SouthEast'    2003-01-23 00:49    530.14    2.1204e+05    NaT                 'winter storm'   
    'SouthEast'    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    'winter storm'   
    'West'         2004-04-06 05:44    434.81    3.4037e+05    2004-04-06 06:10    'equipment fault'
    'MidWest'      2002-03-16 06:18    186.44    2.1275e+05    2002-03-18 23:23    'severe storm'   

Convert the table to a timetable. Specify the second variable with dates and times, RestorationTime, as the time vector of the timetable.

TT = table2timetable(T,'RowTimes','RestorationTime');
TT(1:5,:)
ans=5x5 timetable
     RestorationTime      Region          OutageTime        Loss     Customers           Cause      
    ________________    ___________    ________________    ______    __________    _________________

    2002-02-07 16:50    'SouthWest'    2002-02-01 12:18    458.98    1.8202e+06    'winter storm'   
                        'SouthEast'    2003-01-23 00:49    530.14    2.1204e+05    'winter storm'   
    2003-02-17 08:14    'SouthEast'    2003-02-07 21:15     289.4    1.4294e+05    'winter storm'   
    2004-04-06 06:10    'West'         2004-04-06 05:44    434.81    3.4037e+05    'equipment fault'
    2002-03-18 23:23    'MidWest'      2002-03-16 06:18    186.44    2.1275e+05    'severe storm'   

Convert a table to a timetable by adding a separate time vector that contains the row times. All the table variables become variables of the timetable.

Reading1 = [98;97.5;97.9;98.1;97.9];
Reading2 = [120;111;119;117;116];
T = table(Reading1,Reading2)
T=5x2 table
    Reading1    Reading2
    ________    ________

      98        120     
    97.5        111     
    97.9        119     
    98.1        117     
    97.9        116     

Time = [seconds(1):seconds(1):seconds(5)];
TT = table2timetable(T,'RowTimes',Time)
TT=5x2 timetable
     Time    Reading1    Reading2
    _____    ________    ________

    1 sec      98        120     
    2 sec    97.5        111     
    3 sec    97.9        119     
    4 sec    98.1        117     
    5 sec    97.9        116     

Input Arguments

collapse all

Input table.

Output Arguments

collapse all

Output timetable. The timetable can store metadata such as descriptions, variable units, variable names, and row times. For more information, see the Properties sections of timetable.

Extended Capabilities

Introduced in R2016b

Was this topic helpful?