# 对时间表中的数据进行重采样和聚合

### 导入时间表

```load outdoors outdoors(1:5,:)```
```ans=5×3 timetable Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:24 49 51.3 29.61 2015-11-15 01:30:24 48.9 51.5 29.61 2015-11-15 03:00:24 48.9 51.5 29.61 2015-11-15 04:30:24 48.8 51.5 29.61 2015-11-15 06:00:24 48.7 51.5 29.6 ```

`TF = isregular(outdoors)`
```TF = logical 0 ```

`dt = unique(diff(outdoors.Time))`
```dt = 3x1 duration 00:00:24 01:29:36 01:30:00 ```

### 通过插值对时间表重采样

```TT = retime(outdoors,'hourly','spline'); TT(1:5,:)```
```ans=5×3 timetable Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:00 49.001 51.298 29.61 2015-11-15 01:00:00 48.909 51.467 29.61 2015-11-15 02:00:00 48.902 51.51 29.61 2015-11-15 03:00:00 48.9 51.5 29.61 2015-11-15 04:00:00 48.844 51.498 29.611 ```

### 通过最近邻值对时间表进行重采样

`TT` 指定一个按小时计的时间向量。对于 `TT` 中的每一行，复制 `outdoors` 中行时间最近的对应行中的值。

```TT = retime(outdoors,'hourly','nearest'); TT(1:5,:)```
```ans=5×3 timetable Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:00 49 51.3 29.61 2015-11-15 01:00:00 48.9 51.5 29.61 2015-11-15 02:00:00 48.9 51.5 29.61 2015-11-15 03:00:00 48.9 51.5 29.61 2015-11-15 04:00:00 48.8 51.5 29.61 ```

### 聚合时间表数据并计算日均值

`retime` 函数提供了聚合方法，例如 `mean`。计算 `outdoors` 中的数据的日均值。

```TT = retime(outdoors,'daily','mean'); TT```
```TT=4×3 timetable Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:00 48.931 51.394 29.607 2015-11-16 00:00:00 47.924 51.571 29.611 2015-11-17 00:00:00 48.45 51.238 29.613 2015-11-18 00:00:00 49.5 50.8 29.61 ```

### 将时间表数据调整为规则时间

```TT = retime(outdoors,'regular','mean','TimeStep',hours(6)); TT(1:5,:)```
```ans=5×3 timetable Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:00 48.9 51.45 29.61 2015-11-15 06:00:00 48.9 51.45 29.6 2015-11-15 12:00:00 49.025 51.45 29.61 2015-11-15 18:00:00 48.9 51.225 29.607 2015-11-16 00:00:00 48.5 51.4 29.61 ```

```tv = datetime(2015,11,15):hours(6):datetime(2015,11,18); tv.Format = 'dd-MMM-yyyy HH:mm:ss'; TT = retime(outdoors,tv,'mean'); TT(1:5,:)```
```ans=5×3 timetable Time Humidity TemperatureF PressureHg ____________________ ________ ____________ __________ 15-Nov-2015 00:00:00 48.9 51.45 29.61 15-Nov-2015 06:00:00 48.9 51.45 29.6 15-Nov-2015 12:00:00 49.025 51.45 29.61 15-Nov-2015 18:00:00 48.9 51.225 29.607 16-Nov-2015 00:00:00 48.5 51.4 29.61 ```