timetable.fillmissing
timetable: ttB = fillmissing (ttA, method)
timetable: ttB = fillmissing (ttA, 'constant', v)
timetable: ttB = fillmissing (…, Name, Value)
timetable: [ttB, TF] = fillmissing (…)
Fill the missing values of a timetable.
ttB = fillmissing (ttA, method) replaces each
missing value by one worked out from the values around it.
'previous', 'next' and 'nearest' copy a
neighbouring value; 'linear', 'spline',
'pchip' and 'makima' interpolate; and
'constant' takes the value given after it.
The row times are what the filling runs against, not the
order of the rows. A gap an hour after its left neighbour and two
hours before its right one is filled a third of the way between them
by 'linear', and takes the left value under
'nearest', where counting rows would put it midway and call
the two neighbours equally close. The row times are already the
sample points, so 'SamplePoints' is not accepted; a timetable
whose row times are not all known is refused outright rather than
filled around the gap.
'DataVariables' names the variables to fill and
'EndValues' says what to do with a gap that has no neighbour
on one side, taking 'extrap', another method name, or a
constant.
[ttB, TF] = fillmissing (…) also returns a
logical array marking what was filled. The row times themselves are
never filled and the time step is unchanged.
See also: ismissing, rmmissing, standardizeMissing, timetable
Source Code: timetable
fillmissing runs against the row times, not the order of the rows. The gap below sits an hour after its left neighbour and two hours before its right one, so 'linear' fills it a third of the way between them. Counting rows would have put it midway, at 20.
t0 = datetime (2024, 1, 1); Depth = [10; NaN; 30]; TT = timetable (Depth, 'RowTimes', t0 + hours ([0 1 3])')
TT =
3x1 timetable
Time Depth
____________________ _____
01-Jan-2024 00:00:00 10
01-Jan-2024 01:00:00 NaN
01-Jan-2024 03:00:00 30
fillmissing (TT, 'linear')
ans =
3x1 timetable
Time Depth
____________________ _______
01-Jan-2024 00:00:00 10
01-Jan-2024 01:00:00 16.6667
01-Jan-2024 03:00:00 30
The same spacing decides which neighbour is nearest. Here the earlier reading is an hour away and the later one two, so 'nearest' takes the earlier; by row count the two would be equally close.
t0 = datetime (2024, 1, 1);
TT = timetable ([10; NaN; 30], 'RowTimes', t0 + hours ([0 1 3])', ...
'VariableNames', {'Depth'});
fillmissing (TT, 'nearest').Depth
ans = 10 10 30
fillmissing (TT, 'next').Depth
ans = 10 30 30
fillmissing (TT, 'constant', 0).Depth
ans =
10
0
30
Because the row times are already the sample points, 'SamplePoints' is not accepted, and a timetable that cannot say when a row happened is refused rather than filled around the gap.
t0 = datetime (2024, 1, 1);
TT = timetable ([10; NaN; 30], 'RowTimes', [t0; NaT; t0 + hours(3)], ...
'VariableNames', {'Depth'});
try
fillmissing (TT, 'previous')
catch err
disp (err.message)
end
timetable.fillmissing: row times must not be missing.