Main Content

table2timetable

Convert table to timetable

描述

example

TT= table2timetable(T)converts the tableT到时间表。第一个DateTime或持续时间变量Tbecomes the vector of row times ofTT。The remaining variables ofTbecome the variables ofTT

  • 如果T是一个M-by-Ntable without row names, thenTT是一个M-by-(N-1)timetable.

  • 如果T是一个M-by-Ntable with row names, thentable2timetableassigns the row names ofT到一个变量TT。结果是,TT是一个M-by-Ntimetable.

有关创建和使用时间表的详细信息,请参阅时间表

To create a timetable from data in a text or spreadsheet file, first use the可阅读function to read the data to a table. Then convert the table to a timetable withtable2timetable

example

TT= table2timetable(T,'RowTimes',timevarname.)分配表变量timevarname.as the vector of row times of the output timetable.timevarname.can be the name of any variable inTthat contains datetime or duration values. The remaining variables ofTbecome the variables ofTT

example

TT= table2timetable(T,'RowTimes',rowTimes)assigns the vectorrowTimesas the vector of row times of the output timetable. All of the variables ofT变成了变量TT

example

TT= table2timetable(T,'采样率',FS.)uses the sample rateFS.计算规则间隔的行时间。FS.是一个正数字标量,指定每秒的样本数(Hz)。第一行时间为零秒。

example

TT= table2timetable(T,'timestep',dt)uses the time stepdt计算规则间隔的行时间。dt是一个持续时间或日历持续时间值,它指定连续行时间之间的时间长度。第一行时间为零秒。

example

TT= table2timetable(___,'开始时间',t0)指定开始时间t0,而不是零秒作为第一行时间。您可以使用此语法使用此语法使用常规时间表使用'采样率'or'TimeStep'名称-值pair arguments from either of the previous two syntaxes.

例子

全部收缩

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

从文件中读取断电数据中断.CSVto a table. The table contains both outage and restoration times.

t = readtable('outages.csv');T(1:5,:)
ans =.5×6 tableRegion 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 ofTT

TT = table2timetable(T); TT(1:5,:)
ans =.5×5 timetableOutageTime地区损失客户恢复时间_____________________________________________________________________________________________________________________________07 {'冬季风暴'} 2003-01-23 00:49 {'southeast'} 530.14 2.1204E + 05 Nat {'冬季风暴'} 2003-02-07 21:15 {'southeast'} 289.4 1.4294E + 05 2003-02-17 08:14 {'冬季风暴'2004-04-06 05:44 {'{West'} 434.81 3.4037E + 05 2004-04-06 06:10 {'设备故障'} 2002-03-16 06:18 {'MIDWEST'} 186.44 2.1275E +05 2002-03-18 23:23 {'严重风暴'}

索引进入TTusing row times from its time vector. You can treat the row times as labels that specify rows.

TT('2003-02-07 21:15',:)
ans =.1×5 timetableOutageTime 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.utagingtion = tt.restiontime  -  tt.outageTime;TT(1:5,:)
ans =.5×6时间表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.

从文件中读取断电数据中断.CSVto a table. The table contains both outage and restoration times.

t = readtable('outages.csv');T(1:5,:)
ans =.5×6 tableRegion 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作为时间表的时间向量。

tt = table2timetable(t,'RowTimes','RestorationTime');TT(1:5,:)
ans =.5×5 timetableRestorationTime Region OutageTime Loss Customers Cause ________________ _____________ ________________ ______ __________ ___________________ 2002-02-07 16:50 {'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 {'winter storm' } NaT {'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.

读数1 = [98; 97.5; 97.9; 98.1; 97.9];读数2 = [120; 111; 119; 117; 116];t =表(读数1,读数2)
T=5×2表读数1读数2 ________ ________ 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=5×2 timetable时间读数1读数2 _____ _________ ________ 1秒98 120 2秒97.5 111 3秒97.9 119 4秒98.1 117 5秒97.9 116

Create a table.

读数1 = [98; 97.5; 97.9; 98.1; 97.9];读数2 = [120; 111; 119; 117; 116];t =表(读数1,读数2)
T=5×2表读数1读数2 ________ ________ 98 120 97.5 111 97.9 119 98.1 117 97.9 116

Specify a sample rate of 500 Hz. ConvertTto a timetable using that sample rate and a starting row time of zero seconds.

tt = table2timetable(t,'采样率',500)
TT=5×2 timetable时间读数1读数2 _________ _________________ 0秒98 120 0.002秒97.5 111 0.004秒97.9 119 0.006秒98.1 117 0.008秒97.9 116

Create a table.

读数1 = [98; 97.5; 97.9; 98.1; 97.9];读数2 = [120; 111; 119; 117; 116];t =表(读数1,读数2)
T=5×2表读数1读数2 ________ ________ 98 120 97.5 111 97.9 119 98.1 117 97.9 116

Specify a time step of ten seconds, using thefunction. ConvertTto a timetable using that sampling rate and a starting row time of zero seconds.

tt = table2timetable(t,'TimeStep',seconds(10))
TT=5×2 timetableTime Reading1 Reading2 ________ ________ ________ 00:00:00 98 120 00:00:10 97.5 111 00:00:20 97.9 119 00:00:30 98.1 117 00:00:40 97.9 116

Create a table.

读数1 = [98; 97.5; 97.9; 98.1; 97.9];读数2 = [120; 111; 119; 117; 116];t =表(读数1,读数2)
T=5×2表读数1读数2 ________ ________ 98 120 97.5 111 97.9 119 98.1 117 97.9 116

通过指定十秒钟的时间步长和5秒的开始时间将其转换为时间表。

dt =秒(10);t0 =秒(5);tt = table2timetable(t,'TimeStep',dt,'StartTime',t0)
TT=5×2 timetableTime Reading1 Reading2 ______ ________ ________ 5 sec 98 120 15 sec 97.5 111 25 sec 97.9 119 35 sec 98.1 117 45 sec 97.9 116

输入参数

全部收缩

输入表。

Name of a variable from the input table, specified as a character vector or a string scalar.

Row times assigned to the output timetable, specified as a datetime vector or a duration vector. The number of elements ofrowTimes必须等于输入表的行数。时间值rowTimesdo not need to be unique, sorted, or regular.

采样率,指定为正数字标量。FS.specifies the number of samples per second (Hz).

Time step, specified as a datetime scalar or duration scalar.

数据类型:datetime|持续时间|日历

Start time, specified as a datetime scalar or duration scalar.

  • 如果t0是DateTime值,然后是行时间TT是日期时间值。

  • 如果t0is a duration, then the row times are durations.

如果时间步dtis a calendar duration value, thent0must be a datetime value.

数据类型:datetime|持续时间

输出参数

全部收缩

输出时间表。时间表可以存储元数据,例如描述,可变单元,变量名和行时间。有关更多信息,请参阅属性部分timetable

Tips

  • 在某些情况下,您可以打电话table2timetable使用语法指定行时间之间的常规时间步长table2timetablereturns an irregular timetable. This result occurs when you specify the time step using a calendar unit of time and there is a row time that introduces an irregular step. For example, if you create a timetable with a time step of one calendar month, starting on January 31, 2019, then it is irregular with respect to months.

    stime = datetime(2019,1,31); tstep = calmonths(1); T = table([1:3]'); TT = table2timetable(T,'TimeStep',tstep,'StartTime',stime)
    TT = 3×1 timetable Time Var1 ___________ ____ 31-Jan-2019 1 28-Feb-2019 2 31-Mar-2019 3

    还有其他情况,其中违规是由于夏令时(DST)的偏移或闰秒的行时间。此表指定了可能意外地产生不规则时间表的行时间值和时间步骤。

    Row Time Value

    Time Step

    开始时间指定为本月的第29,30或第31天。

    Number of calendar months or quarters.

    开始时间指明为2月29日。

    Number of calendar years.

    任何行时间在上午1点到下午2点到2:00。在一天中从DST转移到标准时间(当行时间指定为时区观察DST时的日期时间值时)。 日历日或月数。

    闰秒的任何行时间(当行时间指定为时区的日期时间值时utcleapseconds.time zone). For the list of leap seconds, seeleapseconds

    在任何日历单元(天,周,月,季度或年份)中指定的时间步骤。

Compatibility Considerations

展开全部

Not recommended starting in R2018b

扩展能力

Introduced in R2016b