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 theVariableContinuity
property of the timetable. When you resample the timetable using theretime
function,retime
either interpolates, fills in with previous values, or fills in with missing data indicators, depending on the values in theVariableContinuity
property. Similarly, thesynchronize
function interpolates or fills in values based on theVariableContinuity
property of the input timetables.
Create a timetable that has simulated weather measurements for several days in May 2017. The timetable variablesTmax
andTmin
contain maximum and minimum temperature readings for each day, andPrecipTotal
contains total precipitation for the day.WXEvent
is 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
One way to fill in data for the two missing days is to use theretime
function. If you callretime
without specifying a method, thenretime
fills in gaps with missing data indicators. For instance,retime
fills gaps in numeric variables withNaN
values, 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 NaN07-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 callretime
multiple times, each time indexing into the timetable to access a different subset of variables.
However, you also can apply different methods by specifying theVariableContinuity
property of the timetable. You can specify whether each variable contains continuous or discrete data. Then theretime
function applies a different method to each timetable variable, depending on the correspondingVariableContinuity
value.
If you specifyVariableContinuity
, then theretime
function fills in the output timetable variables using the following methods:
'unset'
— Fill in values using the missing data indicator for that type (such asNaN
for 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 inStation1
is continuous, thatPrecipTotal
is step data, and thatWXEvent
is 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
, theretime
function interpolates the temperature data, fills in the previous day's values inPrecipTotal
, and fills inWXEvent
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 60 43.333 007-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, thenretime
applies 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 NaN07-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
Thesynchronize
function also fills in output timetable variables using different methods, depending on the values specified in theVariableContinuity
property 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 thesynchronize
function.synchronize
fills in values for variables fromStation1
according to the values in theVariableContinuity
property ofStation1
。However, since theVariableContinuity
property ofStation2
is empty,synchronize
fills inPressure
withNaN
values.
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 01013 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.Pressure
contains continuous data, specify theVariableContinuity
property ofStation2
。ThoughStation2
contains only one variable, you must specifyVariableContinuity
using 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.synchronize
fills in values inBothStations.Pressure
becauseStation2.Pressure
has 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 01013 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
, thensynchronize
applies that method to all variables, just as theretime
function does.