Main Content

retime

Resample or aggregate data in timetable, and resolve duplicate or irregular times

Description

example

TT2 = retime(TT1,newTimeStep,method)returns a timetable that contains the variables fromTT1and row times that are regularly spaced by the time stepnewTimeStep. Theretimefunction resamples or aggregates data in the variables ofTT1using the function specified bymethod. You can useretimeto:

  • Interpolate data values fromTT1at different times.

  • Aggregate data into time bins (for example, to create a timetable containing quarterly means from monthly data).

  • Remove rows fromTT1that have duplicate row times.

  • Make an irregular timetable into a regular timetable, sincenewTimeStepspecifies regular row times.

ThenewTimeStep那输入向量参数是一个字符或字符串t specifies a predefined time step. For example, whennewTimeStepis'daily', andmethodis'mean', thenTT2contains the daily means of the data fromTT1.

The first row time ofTT2is on the time step before the earliest row time fromTT1. The row times inTT2cover the range of row times fromTT1. However,TT2might not include any of the actual row times fromTT1, sinceTT1might not have any row times that fall on any of the regular row times ofTT2.

To interpolate or fill in values inTT2using different methods for different variables, specify theVariableContinuityproperty ofTT1. For more information, seeRetime and Synchronize Timetable Variables Using Different Methods.

To resample or aggregate data from multiple timetables, seesynchronize.

example

TT2 = retime(TT1,'regular',method,'TimeStep',dt)calculates regularly spaced row times using the time stepdt. Thedtinput argument is a scalar duration or calendar duration, specifying a time step of any size. The row times ofTT2span the range of row times ofTT1.

Use this syntax when the time step is not one of the predefined time steps you can specify as a character vector or string.

example

TT2 = retime(TT1,'regular',method,'SampleRate',Fs)calculates regularly spaced row times using the sample rateFs. TheFsinput argument is a positive numeric scalar that specifies the number of samples per second (Hz).

example

TT2 = retime(TT1,newTimes,method)adjusts the timetable variables data to the time vectornewTimes, using the method specified bymethod. ThenewTimestime vector can be irregular, but it must be a sorted datetime or duration vector and contain unique values. The times innewTimesbecome the row times ofTT2.

example

TT2 = retime(TT1,newTimeStep)adjusts timetable data using the'fillwithmissing'method.TT2has missing data indicators whereverTT2has a row time that does not match any row time inTT1.

IfTT1has rows with duplicate row times andTT2has row times that match the duplicates, thenTT2contains the first row from each group of rows inTT1with duplicate row times that match.

TT2 = retime(TT1,'regular','TimeStep',dt)calculates regularly spaced row times using the time stepdtand, where needed, inserts missing data indicators.

TT2 = retime(TT1,'regular','SampleRate',Fs)calculates regularly spaced row times using the sample rateFsand, where needed, inserts missing data indicators.

TT2 = retime(TT1,newTimes)returns a timetable containing missing data indicators wherevernewTimesdoes not match row times inTT1.

example

TT2 = retime(___,Name,Value)adjusts timetable data using additional options specified by one or moreName,Valuepairs. You can use this syntax with the input arguments of any of the previous syntaxes.

Examples

collapse all

Create timetable data that are approximately hourly, but with some irregularity in the times. Interpolate the data so that the output timetable has regular hourly row times.

Time = datetime({'2015-12-18 07:02:12';'2015-12-18 08:00:47';...'2015-12-18 09:01:37';'2015-12-18 10:03:10';...'2015-12-18 10:59:34'}); Temp = [37.3;41.9;45.7;42.3;39.8]; Pressure = [30.1;29.9;30.03;29.9;29.8]; TT = timetable(Time,Temp,Pressure)
TT=5×2 timetableTime Temp Pressure ____________________ ____ ________ 18-Dec-2015 07:02:12 37.3 30.1 18-Dec-2015 08:00:47 41.9 29.9 18-Dec-2015 09:01:37 45.7 30.03 18-Dec-2015 10:03:10 42.3 29.9 18-Dec-2015 10:59:34 39.8 29.8

To resample with cubic spline interpolation, specify'spline'.

TT2 = retime(TT,'hourly','spline')
TT2=5×2 timetableTime Temp Pressure ____________________ ______ ________ 18-Dec-2015 07:00:00 37.228 30.124 18-Dec-2015 08:00:00 41.824 29.899 18-Dec-2015 09:00:00 45.694 30.029 18-Dec-2015 10:00:00 42.552 29.91 18-Dec-2015 11:00:00 39.808 29.8

Create a timetable with temperature and pulse readings taken every fifteen minutes.

Time = [minutes(0):minutes(15):minutes(105)]'; Temp = [98;97.5;97.9;98.1;97.9;98;98.3;97.8]; Pulse = [80;75;73;68;69;65;72;71]; TT = timetable(Time,Temp,Pulse)
TT=8×2 timetableTime Temp Pulse _______ ____ _____ 0 min 98 80 15 min 97.5 75 30 min 97.9 73 45 min 98.1 68 60 min 97.9 69 75 min 98 65 90 min 98.3 72 105 min 97.8 71

Calculate the mean for each reading over hourly time bins. When you aggregate data over time bins, the row times of the output timetable are the left edges of the time bins.

TT2 = retime(TT,'hourly','mean')
TT2=2×2 timetableTime Temp Pulse ______ ______ _____ 0 min 97.875 74 60 min 98 69.25

Create a timetable that contains times, temperature, and pressure readings taken approximately at the half-hour mark, but with one measurement from 9:00 AM missing.

Time = datetime({'2015-12-18 07:29:53';'2015-12-18 08:00:00';...'2015-12-18 08:31:02';'2015-12-18 09:30:00'}); Temp = [37.3;41.9;45.7;39.8]; Pressure = [30.1;29.9;30.03;29.8]; TT1 = timetable(Time,Temp,Pressure)
TT1=4×2 timetableTime Temp Pressure ____________________ ____ ________ 18-Dec-2015 07:29:53 37.3 30.1 18-Dec-2015 08:00:00 41.9 29.9 18-Dec-2015 08:31:02 45.7 30.03 18-Dec-2015 09:30:00 39.8 29.8

Specify a 30 minute time step. Since 30 minutes is not a predefined time step, you must specify it as a duration value, using the'TimeStep'name-value pair argument. Resample the data fromTT1using linear interpolation.

dt = minutes(30); TT2 = retime(TT1,'regular','linear','TimeStep',dt)
TT2=6×2 timetableTime Temp Pressure ____________________ ______ ________ 18-Dec-2015 07:00:00 32.736 30.298 18-Dec-2015 07:30:00 37.318 30.099 18-Dec-2015 08:00:00 41.9 29.9 18-Dec-2015 08:30:00 45.573 30.026 18-Dec-2015 09:00:00 42.802 29.917 18-Dec-2015 09:30:00 39.8 29.8

Create a timetable using column vectors of data. The row times are between 10 and 50 milliseconds.

Intensity = [100 98.7 95.2 101.4 99.1]'; Time = milliseconds([11 20 34 40.3 49.9])'; TT1 = timetable(Time,Intensity)
TT1=5×1 timetableTime Intensity __________ _________ 0.011 sec 100 0.02 sec 98.7 0.034 sec 95.2 0.0403 sec 101.4 0.0499 sec 99.1

Resample the data inTT1using a sample rate of 100 Hz.

TT2 = retime(TT1,'regular','linear','SampleRate', 100)
TT2=5×1 timetableTime Intensity ________ _________ 0.01 sec 100.14 0.02 sec 98.7 0.03 sec 96.2 0.04 sec 101.1 0.05 sec 99.076

Create a timetable that contains times, temperature, and pressure readings.

Time = datetime({'2015-12-18 07:29:53';'2015-12-18 08:00:00';...'2015-12-18 08:31:02';'2015-12-18 09:30:00'}); Temp = [37.3;41.9;45.7;39.8]; Pressure = [30.1;29.9;30.03;29.8]; TT1 = timetable(Time,Temp,Pressure)
TT1=4×2 timetableTime Temp Pressure ____________________ ____ ________ 18-Dec-2015 07:29:53 37.3 30.1 18-Dec-2015 08:00:00 41.9 29.9 18-Dec-2015 08:31:02 45.7 30.03 18-Dec-2015 09:30:00 39.8 29.8

Create a time vector and interpolate the timetable data at the times in the vector. This time vector starts at 8:00 AM. If you instead use the'TimeStep'name-value pair argument, then the output timetable would start at 7:00 AM. One reason to use a time vector is to ensure that the output timetable starts, and ends, with times you specify.

newTimes = [datetime('2015-12-18 08:00:00'):minutes(30):datetime('2015-12-18 09:30:00')]; TT2 = retime(TT1,newTimes,'linear')
TT2=4×2 timetableTime Temp Pressure ____________________ ______ ________ 18-Dec-2015 08:00:00 41.9 29.9 18-Dec-2015 08:30:00 45.573 30.026 18-Dec-2015 09:00:00 42.802 29.917 18-Dec-2015 09:30:00 39.8 29.8

Create a timetable that contains times and measurements of wind speed and direction.

Time = datetime({'2015-12-18 07:00:00';'2015-12-18 08:03:47';...'2015-12-18 09:00:00';'2015-12-18 10:00:00';...'2015-12-18 10:59:34'}); WindSpeed = [13.4;6.5;7.3;8.5;2.3]; WindDirection = categorical({'NE';“N”;'NE';'NW';'W'}); TT1 = timetable(Time,WindSpeed,WindDirection)
TT1=5×2 timetableTime WindSpeed WindDirection ____________________ _________ _____________ 18-Dec-2015 07:00:00 13.4 NE 18-Dec-2015 08:03:47 6.5 N 18-Dec-2015 09:00:00 7.3 NE 18-Dec-2015 10:00:00 8.5 NW 18-Dec-2015 10:59:34 2.3 W

Adjust the data to an hourly time vector. Insert missing data indicators inTT2whereTT1does not have data on the hourly time vector.

TT2 = retime(TT1,'hourly')
TT2=5×2 timetableTime WindSpeed WindDirection ____________________ _________ _____________ 18-Dec-2015 07:00:00 13.4 NE 18-Dec-2015 08:00:00 NaN  18-Dec-2015 09:00:00 7.3 NE 18-Dec-2015 10:00:00 8.5 NW 18-Dec-2015 11:00:00 NaN 

Load a timetable. Adjust the timetable variables using theretimefunction and different methods for different variables.

Load a timetable with temperature, wind speed, and rainfall measurements for Boston.

loadbostonTTBoston
Boston=6×3 timetableTime Temp WindSpeed Rain ___________________ ____ _________ ____ 2016-06-09 06:03:00 59.5 0.1 0.05 2016-06-09 12:00:23 63 2.3 0.08 2016-06-09 18:02:57 61.7 3.1 0.13 2016-06-10 06:01:47 55.4 5.7 0.15 2016-06-10 12:06:00 62.3 2.6 0.87 2016-06-10 18:02:57 58.8 6.2 0.33

Adjust the data to produce daily mean temperatures and wind speeds, and daily sums of the rainfall.retimeapplies the same method to all timetable variables. To apply different methods, index into the timetable to select variables, and callretimefor each method you use.

BOS = Boston(:,{'Temp','WindSpeed'}); TT1 = retime(BOS,'daily','mean')
TT1=2×2 timetableTime Temp WindSpeed ___________________ ______ _________ 2016-06-09 00:00:00 61.4 1.8333 2016-06-10 00:00:00 58.833 4.8333
BOS = Boston(:,'Rain'); TT2 = retime(BOS,'daily','sum')
TT2=2×1 timetableTime Rain ___________________ ____ 2016-06-09 00:00:00 0.26 2016-06-10 00:00:00 1.35

To combine all results in one timetable, concatenateTT1andTT2.

TT = [TT1 TT2]
TT=2×3 timetableTime Temp WindSpeed Rain ___________________ ______ _________ ____ 2016-06-09 00:00:00 61.4 1.8333 0.26 2016-06-10 00:00:00 58.833 4.8333 1.35

Bin timetable data into hourly bins. Specify the right edges, or end times, of the time bins as row times. By default, the left edges, or start times of the time bins, are row times.

Create a timetable with temperature and pulse readings taken at fifteen minutes intervals.

Time = [minutes(15):minutes(15):minutes(105)]'; Temp = [97.5;97.9;98.1;97.9;98;98.3;97.8]; Pulse = [75;73;68;69;65;72;71]; TT = timetable(Time,Temp,Pulse)
TT=7×2 timetableTime Temp Pulse _______ ____ _____ 15 min 97.5 75 30 min 97.9 73 45 min 98.1 68 60 min 97.9 69 75 min 98 65 90 min 98.3 72 105 min 97.8 71

Calculate the mean for each reading over hourly time bins. Specify that the row times of the output timetable are the right edges of the time bins. Since the right edges are included, the reading at 60 minutes is included in the first time bin.

TT2 = retime(TT,'hourly','mean','IncludedEdge','right')
TT2=2×2 timetableTime Temp Pulse _______ ______ ______ 60 min 97.85 71.25 120 min 98.033 69.333

Calculate the means, with the left edges as the row times. The mean values are different from those inTT2, because the reading at 60 minutes is now in the second time bin.

TT3 = retime(TT,'hourly','mean')
TT3=2×2 timetableTime Temp Pulse ______ ______ _____ 0 min 97.833 72 60 min 98 69.25

Input Arguments

collapse all

输入时间表。

Time step for spacing times in the output timetable, specified as a character vector.newTimeStepcan be any of the predefined time steps in the table.

Time Step

Description

'yearly'

One year

'quarterly'

One quarter

'monthly'

One month

'weekly'

One week

'daily'

One day

'hourly'

One hour

'minutely'

One minute

'secondly'

One second

Time step of any size, specified as a datetime scalar or duration scalar.

Data Types:datetime|duration|calendarDuration

Sample rate, specified as a positive numeric scalar.Fsspecifies the number of samples per second (Hz).

New time vector, specified as a datetime vector or a duration vector. The new time vector must be a column vector.newTimescan have a different number of rows thanTT1.

Method for adjusting timetable data, specified as a character vector, string scalar, or function handle. You can use any of the listed methods listed to adjust the data fromTT1.

Fill Methods

Copy data from the rows ofTT1when row times ofTT2match row times ofTT1. Then, fill the remaining rows ofTT2with missing data indicators.

To fill the remaining rows with a constant instead of a missing data indicator, specifymethodas'fillwithconstant'and use the'Constant'name-value pair argument.

Method

Description

'fillwithmissing'

Fill gaps with missing data indicators (for example,NaNfor numeric variables).

'fillwithconstant'

Fill gaps with the value of the'Constant'name-value pair argument. The default value is0.

Nearest Neighbor Methods

Copy data from the rows ofTT1to the rows ofTT2whose row times are the nearest match, according to the specified method.TT1must be sorted by its row times.

Method

Description

'previous'

复制数据从最近的前邻居the input timetable, proceeding from the end of the vector of row times. If there are duplicate row times, then'previous'indicates the last of the duplicates.

'next'

Copy data from the nearest following neighbor in the input timetable, proceeding from the beginning of the vector of row times. If there are duplicate row times, then'next'indicates the first of the duplicates.

'nearest'

Copy data from the nearest neighbor in the input timetable.

Interpolation Methods

Interpolate data values inTT2from data values in neighboring rows ofTT1. The input timetable must have row times that are sorted and unique. To control how the data are extrapolated beyond the first and last row times ofTT1, use the'EndValues'name-value pair argument.

Method

Description

'linear'

Use linear interpolation.

'spline'

Use piecewise cubic spline interpolation.

'pchip'

Use shape-preserving piecewise cubic interpolation.

'makima'

Use modified Akima cubic Hermite interpolation.

Aggregation Methods

Aggregate data from the rows ofTT1over time bins specified by the row times ofTT2. Each row time ofTT2时间本的左边缘,下一个缺点ecutive row time being the right edge. By default, the left edges are included in the time bins. To control whether the left or the right bin edges are included in the time bins, use the'IncludedEdge'name-value pair argument.

If you specify the time vectornewTimes, thennewTimesmust be sorted in ascending order.

All the listed methods omitNaNs,NaTs, and other missing data indicators, except forfunc. To include missing data indicators, specifyfuncas a function handle to a function that includes them when aggregating data.

Method

Description

'sum'

Sum the values in each time bin.

'prod'

Calculate the product of the values in each time bin.

'mean'

Calculate the mean of the values in each time bin.

'median'

Calculate the median of the values in each time bin.

'mode'

Calculate the mode of the values in each time bin.

'min'

Calculate the minimum of the values in each time bin.

'max'

Calculate the maximum of the values in each time bin.

'count'

Count the number of values in each time bin.

'firstvalue'

Use the first value in each time bin.

'lastvalue'

Use the last value in each time bin.

@func

Use the function specified by the function handle (for example,@stdto calculate the standard deviation for the values in each time bin).funcmust return an output argument that is a scalar or a row vector, and must accept empty inputs.

Default Method

The default method is equivalent to leavingmethodunspecified.

Method

Description

'default'(default)

Either fill gaps with missing data indicators, or use per-variable methods if they are specified by theVariableContinuityproperty of the input timetable.

Name-Value Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:TT2 = retime(TT1,newTimes,'Constant',-1)creates the timetableTT2and assigns the value-1to elements in rows ofTT2with row times that do not match row times fromTT1.

Value for filling gaps when the method is'fillwithconstant', specified as the comma-separated pair consisting of'Constant'and an array. The default value is0. The data type of the value specified by'Constant'must be compatible with the data types of the timetable variables.

Example:TT2 = retime(TT1,'hourly','fillwithconstant','Constant','NONE')fills gaps inTT2with the character vector'NONE'when all the variables inTT2contain text.

Method for extrapolation when using an interpolation method, specified as the comma-separated pair consisting of'EndValues'and either'extrap'or a scalar. If you specify a scalar, then its data type must be compatible with all the timetable variables.

Method

Description

'extrap'(default)

Extrapolate using the method specified by themethodinput argument

scalar

Extrapolate by filling gaps outside the range of input row times with a scalar

Example:TT2 = retime(TT1,'daily','previous','EndValues',1000)fills gaps inTT2with previous row values whereTT2has row times within the range of row times fromTT1, and with the value1000whereTT2has row times outside that range.

Edges to include in each time bin, specified as the comma-separated pair consisting of'IncludedEdge'and either'left'or'right'. Each row time ofTT2时间本的左边缘,下一个缺点ecutive row time being the right edge.

Edges to Include

Description

'left'(default)

All bins include the left bin edge, except for the last bin, which includes both edges

'right'

All bins include the right bin edge, except for the first bin, which includes both edges

If you specify'left', then the time bins include the left edges except for the last bin, which includes both edges. If you specify'right', then the time bins include the right edges except for the first bin, which includes both edges.

Example:TT2 = retime(TT1,'hourly','mean','IncludedEdge','right')includes the right bin edge of each time bin.

Compatibility Considerations

expand all

Warns starting in R2021a

Extended Capabilities

Introduced in R2016b