Main Content

Retime and Synchronize Timetable Variables Using Different Methods

This example shows how to fill in gaps in timetable variables, using different methods for different variables. You can specify whether each timetable variable contains continuous or discrete data using theVariableContinuityproperty of the timetable. When you resample the timetable using theretimefunction,retimeeither interpolates, fills in with previous values, or fills in with missing data indicators, depending on the values in theVariableContinuityproperty. Similarly, thesynchronizefunction interpolates or fills in values based on theVariableContinuityproperty of the input timetables.

Create Timetable

Create a timetable that has simulated weather measurements for several days in May 2017. The timetable variablesTmaxandTmincontain maximum and minimum temperature readings for each day, andPrecipTotalcontains total precipitation for the day.WXEventis a categorical array, recording whether certain kinds of weather events, such as thunder or hail, happened on any given day. The timetable has simulated data from May 4 to May 10, 2017, but is missing data for two days, May 6th and May 7th.

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 timetableDate 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

Resample Continuous and Discrete Timetable Variables

One way to fill in data for the two missing days is to use theretimefunction. If you callretimewithout specifying a method, thenretimefills in gaps with missing data indicators. For instance,retimefills gaps in numeric variables withNaNvalues, and gaps in the categorical variable with undefined elements.

Station1Daily = retime(Station1,'daily')
Station1Daily=7×4 timetableDate 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  07-May-2017 NaN NaN NaN  08-May-2017 56 40 0 None 09-May-2017 59 42 0.15 Thunder 10-May-2017 60 45 0 None

If you specify a method when you callretime, it uses the same method to fill gaps in every variable. To apply different methods to different variables, you can callretimemultiple times, each time indexing into the timetable to access a different subset of variables.

However, you also can apply different methods by specifying theVariableContinuityproperty of the timetable. You can specify whether each variable contains continuous or discrete data. Then theretimefunction applies a different method to each timetable variable, depending on the correspondingVariableContinuityvalue.

If you specifyVariableContinuity, then theretimefunction fills in the output timetable variables using the following methods:

  • 'unset'— Fill in values using the missing data indicator for that type (such asNaNfor numeric variables).

  • 'continuous'— Fill in values using linear interpolation.

  • 'step'— Fill in values using previous value.

  • 'event'— Fill in values using the missing data indicator for that type.

Specify that the temperature data inStation1is continuous, thatPrecipTotalis step data, and thatWXEventis event data.

Station1.Properties.VariableContinuity = {'continuous','continuous','step','event'}; Station1.Properties
ans = TimetableProperties属性: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 CustomProperties: No custom properties are set. Use addprop and rmprop to modify CustomProperties.

Resample the data inStation1。考虑到指定值VariableContinuity, theretimefunction interpolates the temperature data, fills in the previous day's values inPrecipTotal, and fills inWXEventwith undefined elements.

Station1Daily = retime(Station1,'daily')
Station1Daily=7×4 timetableDate 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  07-May-2017 58 41.667 0  08-May-2017 56 40 0 None 09-May-2017 59 42 0.15 Thunder 10-May-2017 60 45 0 None

If you specify a method, thenretimeapplies that method to all variables, overriding the values inVariableContinuity

Station1Missing = retime(Station1,'daily','fillwithmissing')
Station1Missing=7×4 timetableDate 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  07-May-2017 NaN NaN NaN  08-May-2017 56 40 0 None 09-May-2017 59 42 0.15 Thunder 10-May-2017 60 45 0 None

Synchronize Timetables That Contain Continuous and Discrete Data

Thesynchronizefunction also fills in output timetable variables using different methods, depending on the values specified in theVariableContinuityproperty of each input timetable.

Create a second timetable that contains pressure readings in millibars from a second weather station. The timetable has simulated readings from May 4 to May 8, 2017.

Date = datetime(2017,5,4:8)'; Pressure = [995 1003 1013 1018 1006]'; Station2 = timetable(Date,Pressure)
Station2=5×1 timetableDate Pressure ___________ ________ 04-May-2017 995 05-May-2017 1003 06-May-2017 1013 07-May-2017 1018 08-May-2017 1006

Synchronize the data from the two stations using thesynchronizefunction.synchronizefills in values for variables fromStation1according to the values in theVariableContinuityproperty ofStation1。However, since theVariableContinuityproperty ofStation2is empty,synchronizefills inPressurewithNaNvalues.

BothStations = synchronize(Station1,Station2)
BothStations=7×5 timetableDate 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  1013 07-May-2017 58 41.667 0  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

To indicate thatStation2.Pressurecontains continuous data, specify theVariableContinuityproperty ofStation2。ThoughStation2contains only one variable, you must specifyVariableContinuityusing a cell array, not a character vector.

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

Synchronize the data from the two stations.synchronizefills in values inBothStations.PressurebecauseStation2.Pressurehas continuous data.

BothStations = synchronize(Station1,Station2)
BothStations=7×5 timetableDate 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  1013 07-May-2017 58 41.667 0  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

If you specify a method as an input argument tosynchronize, thensynchronizeapplies that method to all variables, just as theretimefunction does.

See Also

||

Related Topics