主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

timeseries2timetable

timeseries 对象转换为时间表

自 R2021a 起

说明

TT = timeseries2timetable(ts)timeseries 对象转换为一个时间表。

  • 如果 tstimeseries 对象,则 TT 是具有一个变量的时间表。

  • 如果 ts 是由 timeseries 对象组成的数组,则 TT 是一个包含的变量数与 ts 中的 timeseries 对象数相同的时间表。ts 中的所有 timeseries 对象必须具有相同的采样时间。

  • 如果任一 timeseries 对象具有多个事件,则该函数将这些事件转换为一个事件表,并将该事件表附加到 TT。重复事件会导致事件表中出现重复的行。 (自 R2024b 起)

示例

TT = timeseries2timetable(ts1,...,tsN)timeseries 对象 ts1,...,tsN 转换为一个时间表。TT 中变量的数目与输入数目匹配。所有 timeseries 对象 ts1,...,tsN 必须具有相同的采样时间。

示例

示例

全部折叠

创建一个包含五个随机数的 timeseries 对象,以 10 秒为间隔进行采样。

ts = timeseries(rand(5,1),[0 10 20 30 40])
  timeseries

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

  More properties, Methods

显示 ts 中的时间和数据。

ts.Time
ans = 5×1

     0
    10
    20
    30
    40

ts.Data
ans = 5×1

    0.8147
    0.9058
    0.1270
    0.9134
    0.6324

ts 转换为一个时间表。

TT = timeseries2timetable(ts)
TT=5×1 timetable
     Time      Data  
    ______    _______

    0 sec     0.81472
    10 sec    0.90579
    20 sec    0.12699
    30 sec    0.91338
    40 sec    0.63236

创建一个 timeseries 对象数组。使用相同的采样时间向量,但为时间序列提供不同的名称。使用 rand 函数创建不同的数据值数组。

ts1 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_1");
ts2 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_2");
ts3 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_3");
ts = [ts1 ts2 ts3]
  1×3 timeseries array with properties:

    Events
    Name
    UserData
    Data
    DataInfo
    Time
    TimeInfo
    Quality
    QualityInfo
    IsTimeFirst
    TreatNaNasMissing
    Length

将来自所有 timeseries 对象的数据合并到一个时间表中。数组中的每个时间序列都为该时间表提供一个变量。

TT = timeseries2timetable(ts)
TT=5×3 timetable
     Time     Series_1    Series_2    Series_3
    ______    ________    ________    ________

    0 sec     0.81472     0.09754     0.15761 
    10 sec    0.90579      0.2785     0.97059 
    20 sec    0.12699     0.54688     0.95717 
    30 sec    0.91338     0.95751     0.48538 
    40 sec    0.63236     0.96489     0.80028 

将多个 timeseries 对象转换为一个时间表。

ts1 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_1");
ts2 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_2");
ts3 = timeseries(rand(5,1),[0 10 20 30 40],"Name","Series_3");
TT = timeseries2timetable(ts1,ts2,ts3)
TT=5×3 timetable
     Time     Series_1    Series_2    Series_3
    ______    ________    ________    ________

    0 sec     0.81472     0.09754     0.15761 
    10 sec    0.90579      0.2785     0.97059 
    20 sec    0.12699     0.54688     0.95717 
    30 sec    0.91338     0.95751     0.48538 
    40 sec    0.63236     0.96489     0.80028 

加载三个 timeseries 对象,这些对象包含三个城市交叉路口的交通数据。

load trafficCounts.mat count1 count2 count3

显示第一个 timeseries 对象的属性。这些属性存储交通数据、收集数据的时间和两个事件。事件是 tsdata.event 对象。

count1
  timeseries

  Common Properties:
            Name: 'Intersection1'
            Time: [15x1 double]
        TimeInfo: [1x1 tsdata.timemetadata]
            Data: [15x1 double]
        DataInfo: [1x1 tsdata.datametadata]
          Events: [1x2 tsdata.event]

  More properties, Methods

显示附加到 count1 的第一个事件。

count1.Events(1)
    EventData: []
         Name: 'AMCommute'
         Time: 8
        Units: 'hours'
    StartDate: ''

显示第二个事件。这些事件标记早晚通勤的时间。

count1.Events(2)
    EventData: []
         Name: 'PMCommute'
         Time: 18
        Units: 'hours'
    StartDate: ''

绘制来自三个 timeseries 对象的数据。要创建一个分块图布局以在子图中显示时间序列,请使用 tiledlayout 函数。图显示红点来标记与三个时间序列相关联的事件。

tiledlayout(3,1)
nexttile
plot(count1)
nexttile
plot(count2)
nexttile
plot(count3)

Figure contains 3 axes objects. Axes object 1 with title Time Series Plot:Intersection1, xlabel Time (hours), ylabel Intersection1 contains 2 objects of type line. One or more of the lines displays its values using only markers Axes object 2 with title Time Series Plot:Intersection2, xlabel Time (hours), ylabel Intersection2 contains 2 objects of type line. One or more of the lines displays its values using only markers Axes object 3 with title Time Series Plot:Intersection3, xlabel Time (hours), ylabel Intersection3 contains 2 objects of type line. One or more of the lines displays its values using only markers

将三个 timeseries 对象转换为一个时间表。将行时间的格式设置为 hh:mm 格式。这些事件在三个时间序列中的相同时间发生,因此时间表在 08:0018:00 都有三个事件标签。

TT = timeseries2timetable(count1,count2,count3);
TT.Time.Format = "hh:mm"
TT=15×3 timetable with 6 events
                  Time     Intersection1    Intersection2    Intersection3
                  _____    _____________    _____________    _____________

                  06:00          38               46               76     
                  07:00          61              132              186     
    <3 events>    08:00          75              135              180     
                  09:00          38               88              115     
                  10:00          28               36               55     
                  11:00          12               12               14     
                  12:00          18               27               30     
                  13:00          18               19               29     
                  14:00          17               15               18     
                  15:00          19               36               48     
                  16:00          32               47               10     
                  17:00          42               65               92     
    <3 events>    18:00          57               66              151     
                  19:00          44               55               90     
                  20:00         114              145              257     

该转换还将事件对象转换为一个事件表。该事件表附加到时间表。事件表显示它在两个时间有三个相同的事件。

TT.Properties.Events
ans = 6×1 eventtable
  Event Labels Variable: EventLabels
  Event Lengths Variable: <instantaneous>

    Time     EventLabels
    _____    ___________

    8 hr     "AMCommute"
    8 hr     "AMCommute"
    8 hr     "AMCommute"
    18 hr    "PMCommute"
    18 hr    "PMCommute"
    18 hr    "PMCommute"

您可以清除包含重复事件的事件表。一种方法是使用 unique 函数。

ET = TT.Properties.Events;
ET = unique(ET)
ET = 2×1 eventtable
  Event Labels Variable: EventLabels
  Event Lengths Variable: <instantaneous>

    Time     EventLabels
    _____    ___________

    8 hr     "AMCommute"
    18 hr    "PMCommute"

将清理后的事件表附加到时间表。显示时间表。

TT.Properties.Events = ET
TT=15×3 timetable with 2 events
                 Time     Intersection1    Intersection2    Intersection3
                 _____    _____________    _____________    _____________

                 06:00          38               46               76     
                 07:00          61              132              186     
    AMCommute    08:00          75              135              180     
                 09:00          38               88              115     
                 10:00          28               36               55     
                 11:00          12               12               14     
                 12:00          18               27               30     
                 13:00          18               19               29     
                 14:00          17               15               18     
                 15:00          19               36               48     
                 16:00          32               47               10     
                 17:00          42               65               92     
    PMCommute    18:00          57               66              151     
                 19:00          44               55               90     
                 20:00         114              145              257     

创建一个图,其中包含时间表中的交通数据。首先创建一个新图窗窗口,这样您就不会重用由 tiledlayout 创建的子图。然后,使用 stackedplot 来绘制时间表中的数据。stackedplot 函数将每个时间表变量绘制在一个对应的子图中,并在事件发生的时间处显示黑色竖线。

figure
stackedplot(TT)

Figure contains an object of type stackedplot.

输入参数

全部折叠

输入时间序列,指定为由 timeseries 对象组成的数组。

此函数使用 ts 的部分属性来分配数据或设置时间表中的属性。对于每个 timeseries 属性,该表描述输出时间表中的结果。

输入 timeseries 属性

输出时间表中的结果

Name

指定对应时间表变量的名称。

如果 Name'unnamed'(默认值),则对应的变量名称为 'Data'(当多个时间序列的名称为 'unnamed' 时,为 'Data_1''Data_2' 等,以此类推)。

Data

指定分配给对应时间表变量的数据。

DataInfo.Units

设置对应时间表变量的 VariableUnits 属性。

DataInfo.Interpolation

设置对应时间表变量的 VariableContinuity 属性。

Time

将采样时间转换为时间表的行时间。根据输入的 TimeTimeInfo 属性中的信息,行时间向量可以是 durationdatetime 向量。

TimeInfo.Units

指定行时间的单位。如果时间表行时间向量是 duration 向量,则 TimeInfo.Units 还确定其格式。

TimeInfo.Format

设置行时间的格式。

TimeInfo.StartDate

设置时间表的 StartTime 属性。

TimeInfo.Increment

设置时间表的 TimeStep 属性。

TimeInfo.Start

计算从 TimeInfo.StartDate 的偏移量,以指定时间表的 StartTime 属性。

IsTimeFirst

确定数据是否需要重定向。

UserData

指定分配给时间表的 UserData 属性的数据。

Events

将事件转换为一个事件表中的条目,并将该事件表附加到时间表。 (自 R2024b 起)

输入时间序列中的重复事件会导致事件表中出现重复行。

Quality

警告。

QualityInfo

如果 timeseries 对象具有 Quality 属性,则发出警告。

版本历史记录

在 R2021a 中推出

全部展开