%% 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 the VariableContinuity property of the timetable. When you resample the timetable using the retime function, retime either interpolates, fills in with previous values, or fills in with missing data indicators, depending on the values in the VariableContinuity property. Similarly, the synchronize function interpolates or fills in values based on the VariableContinuity property of the input timetables.
%% Create Timetable
% Create a timetable that has simulated weather measurements for several days in May 2017. The timetable variables Tmax and Tmin contain maximum and minimum temperature readings for each day, and PrecipTotal 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)

%
%% Resample Continuous and Discrete Timetable Variables
% One way to fill in data for the two missing days is to use the retime function. If you call retime without specifying a method, then retime fills in gaps with missing data indicators. For instance, retime fills gaps in numeric variables with NaN values, and gaps in the categorical variable with undefined elements.

Station1Daily = retime(Station1,'daily')

%
% If you specify a method when you call retime, it uses the same method to fill gaps in every variable. To apply different methods to different variables, you can call retime multiple times, each time indexing into the timetable to access a different subset of variables.
% However, you also can apply different methods by specifying the VariableContinuity property of the timetable. You can specify whether each variable contains continuous or discrete data. Then the retime function applies a different method to each timetable variable, depending on the corresponding VariableContinuity value.
% If you specify VariableContinuity, then the retime function fills in the output timetable variables using the following methods:
% 'unset' — Fill in values using the missing data indicator for that type (such as NaN 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 in Station1 is continuous, that  PrecipTotal is step data, and that WXEvent is event data.

Station1.Properties.VariableContinuity = {'continuous','continuous','step','event'};
Station1.Properties

%
% Resample the data in Station1. Given the values assigned to VariableContinuity, the retime function interpolates the temperature data, fills in the previous day's values in PrecipTotal, and fills in WXEvent with undefined elements.

Station1Daily = retime(Station1,'daily')

%
% If you specify a method, then retime applies that method to all variables, overriding the values in VariableContinuity.

Station1Missing = retime(Station1,'daily','fillwithmissing')

%
%% Synchronize Timetables That Contain Continuous and Discrete Data
% The synchronize function also fills in output timetable variables using different methods, depending on the values specified in the VariableContinuity 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)

%
% Synchronize the data from the two stations using the synchronize function. synchronize fills in values for variables from Station1 according to the values in the VariableContinuity property of Station1. However, since the VariableContinuity property of Station2 is empty, synchronize fills in Pressure with NaN values.

BothStations = synchronize(Station1,Station2)

%
% To indicate that Station2.Pressure contains continuous data, specify the VariableContinuity property of Station2. Though Station2 contains only one variable, you must specify VariableContinuity using a cell array, not a character vector.

Station2.Properties.VariableContinuity = {'continuous'};
Station2.Properties

%
% Synchronize the data from the two stations. synchronize fills in values in BothStations.Pressure because Station2.Pressure has continuous data.

BothStations = synchronize(Station1,Station2)

% If you specify a method as an input argument to synchronize, then synchronize applies that method to all variables, just as the retime function does.
% Copyright 2012 The MathWorks, Inc.