Main Content

使用不同的方法对时间表变量重设时间并进行同步

此示例说明如何通过对不同的变量使用不同的方法来填充时间表变量的空缺。您可以使用时间表的 VariableContinuity 属性,指定各个时间表变量包含的是连续数据还是离散数据。当您使用 retime 函数对时间表重采样时,retime 会插入值、使用前面的值填充或使用缺失数据指示符填充,具体取决于 VariableContinuity 属性中的值。同样,synchronize 函数也会根据输入时间表的 VariableContinuity 属性插入或填充值。

创建时间表

创建一个包含 2017 年 5 月中若干天的模拟天气测量值的时间表。时间表变量 TmaxTmin 包含每日最高温度和最低温度的读数,PrecipTotal 包含该日的总降水量。WXEvent 是一个分类数组,用于记录某个给定日期是否发生了特定类型的天气事件,例如雷电或冰雹。该时间表包含从 2017 年 5 月 4 日到 5 月 10 日的模拟数据,但缺失 5 月 6 日和 5 月 7 日这两天的数据。

Date = [datetime(2017,5,4) datetime(2017,5,5) datetime(2017,5,8:10)]';
Tmax = [60 62 56 59 60]';
Tmin = [44 45 40 42 45]';
PrecipTotal = [0.2 0 0 0.15 0]';
WXEvent = [2 0 0 1 0]';
WXEvent = categorical(WXEvent,[0 1 2 3 4],{'None','Thunder','Hail','Fog','Tornado'});
Station1 = timetable(Date,Tmax,Tmin,PrecipTotal,WXEvent)
Station1=5×4 timetable
       Date        Tmax    Tmin    PrecipTotal    WXEvent
    ___________    ____    ____    ___________    _______

    04-May-2017     60      44         0.2        Hail   
    05-May-2017     62      45           0        None   
    08-May-2017     56      40           0        None   
    09-May-2017     59      42        0.15        Thunder
    10-May-2017     60      45           0        None   

对连续和离散的时间表变量重采样

要为缺失的这两天填充数据,可以使用 retime 函数。如果您在未指定方法的情况下调用 retime,则 retime 会使用缺失数据指示符来填充空缺。例如,retime 使用 NaN 值填充数值变量的空缺,使用 undefined 元素填充分类变量的空缺。

Station1Daily = retime(Station1,'daily')
Station1Daily=7×4 timetable
       Date        Tmax    Tmin    PrecipTotal      WXEvent  
    ___________    ____    ____    ___________    ___________

    04-May-2017     60      44         0.2        Hail       
    05-May-2017     62      45           0        None       
    06-May-2017    NaN     NaN         NaN        <undefined>
    07-May-2017    NaN     NaN         NaN        <undefined>
    08-May-2017     56      40           0        None       
    09-May-2017     59      42        0.15        Thunder    
    10-May-2017     60      45           0        None       

如果您在调用 retime 时指定了方法,该函数将使用同一方法来填充每个变量的空缺。要将不同的方法应用于不同的变量,您可以调用 retime 多次,每次都对时间表进行索引以访问不同的变量子集。

但是,您也可以通过指定时间表的 VariableContinuity 属性来应用不同的方法。您可以指定各个变量是包含连续数据还是离散数据。然后,retime 函数将不同的方法应用于各个时间表变量,具体取决于对应的 VariableContinuity 值。

如果您指定 VariableContinuity,则 retime 函数会使用以下方法填充输出时间表变量:

  • 'unset' - 使用该类型的缺失数据指示符(例如对于数值变量使用 NaN)来填充值。

  • 'continuous' - 使用线性插值方法填充值。

  • 'step' - 使用上一个值填充值。

  • 'event' - 使用该类型的缺失数据指示符来填充值。

指定 Station1 中的温度数据为连续数据 (continuous)、PrecipTotal 为时间步数据 (step),WXEvent 为事件数据 (event)。

Station1.Properties.VariableContinuity = {'continuous','continuous','step','event'};
Station1.Properties
ans = 
  TimetableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Date'  'Variables'}
           VariableNames: {'Tmax'  'Tmin'  'PrecipTotal'  'WXEvent'}
    VariableDescriptions: {}
           VariableUnits: {}
      VariableContinuity: [continuous    continuous    step    event]
                RowTimes: [5x1 datetime]
               StartTime: 04-May-2017
              SampleRate: NaN
                TimeStep: NaN
                  Events: []
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

Station1 中的数据重采样。基于赋给 VariableContinuity 的值,retime 函数对温度数据进行插值、在 PrecipTotal 中填充前一天的值,并使用 undefined 元素填充 WXEvent

Station1Daily = retime(Station1,'daily')
Station1Daily=7×4 timetable
       Date        Tmax     Tmin     PrecipTotal      WXEvent  
    ___________    ____    ______    ___________    ___________

    04-May-2017     60         44        0.2        Hail       
    05-May-2017     62         45          0        None       
    06-May-2017     60     43.333          0        <undefined>
    07-May-2017     58     41.667          0        <undefined>
    08-May-2017     56         40          0        None       
    09-May-2017     59         42       0.15        Thunder    
    10-May-2017     60         45          0        None       

如果您指定了方法,则 retime 会将该方法应用于所有变量,并覆盖 VariableContinuity 中的值。

Station1Missing = retime(Station1,'daily','fillwithmissing')
Station1Missing=7×4 timetable
       Date        Tmax    Tmin    PrecipTotal      WXEvent  
    ___________    ____    ____    ___________    ___________

    04-May-2017     60      44         0.2        Hail       
    05-May-2017     62      45           0        None       
    06-May-2017    NaN     NaN         NaN        <undefined>
    07-May-2017    NaN     NaN         NaN        <undefined>
    08-May-2017     56      40           0        None       
    09-May-2017     59      42        0.15        Thunder    
    10-May-2017     60      45           0        None       

同步包含连续和离散数据的时间表

synchronize 函数还可以使用不同的方法填充输出时间表变量,具体取决于在每个输入时间表的 VariableContinuity 属性中指定的值。

创建第二个时间表,其中包含第二个气象站中的压力读数(以毫巴为单位)。该时间表包含从 2017 年 5 月 4 日到 5 月 8 日的模拟读数。

Date = datetime(2017,5,4:8)';
Pressure = [995 1003 1013 1018 1006]';
Station2 = timetable(Date,Pressure)
Station2=5×1 timetable
       Date        Pressure
    ___________    ________

    04-May-2017       995  
    05-May-2017      1003  
    06-May-2017      1013  
    07-May-2017      1018  
    08-May-2017      1006  

使用 synchronize 函数同步这两个气象站中的数据。synchronize 会根据 Station1VariableContinuity 属性的值来填充 Station1 中的变量的值。但是,由于 Station2VariableContinuity 属性为空,synchronize 将使用 NaN 值来填充 Pressure

BothStations = synchronize(Station1,Station2)
BothStations=7×5 timetable
       Date        Tmax     Tmin     PrecipTotal      WXEvent      Pressure
    ___________    ____    ______    ___________    ___________    ________

    04-May-2017     60         44        0.2        Hail              995  
    05-May-2017     62         45          0        None             1003  
    06-May-2017     60     43.333          0        <undefined>      1013  
    07-May-2017     58     41.667          0        <undefined>      1018  
    08-May-2017     56         40          0        None             1006  
    09-May-2017     59         42       0.15        Thunder           NaN  
    10-May-2017     60         45          0        None              NaN  

要指示 Station2.Pressure 包含连续数据,请指定 Station2VariableContinuity 属性。虽然 Station2 只包含一个变量,但您必须使用元胞数组而非字符向量来指定 VariableContinuity

Station2.Properties.VariableContinuity = {'continuous'};
Station2.Properties
ans = 
  TimetableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Date'  'Variables'}
           VariableNames: {'Pressure'}
    VariableDescriptions: {}
           VariableUnits: {}
      VariableContinuity: continuous
                RowTimes: [5x1 datetime]
               StartTime: 04-May-2017
              SampleRate: NaN
                TimeStep: 1d
                  Events: []
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

同步这两个气象站中的数据。由于 Station2.Pressure 包含连续数据,synchronize 将会填充 BothStations.Pressure 中的值。

BothStations = synchronize(Station1,Station2)
BothStations=7×5 timetable
       Date        Tmax     Tmin     PrecipTotal      WXEvent      Pressure
    ___________    ____    ______    ___________    ___________    ________

    04-May-2017     60         44        0.2        Hail              995  
    05-May-2017     62         45          0        None             1003  
    06-May-2017     60     43.333          0        <undefined>      1013  
    07-May-2017     58     41.667          0        <undefined>      1018  
    08-May-2017     56         40          0        None             1006  
    09-May-2017     59         42       0.15        Thunder           994  
    10-May-2017     60         45          0        None              982  

如果您将某个方法指定为 synchronize 的输入参量,则 synchronize 会将该方法应用于所有变量,就像 retime 函数所执行的一样。

另请参阅

| |

相关主题