Resample or aggregate timetable data in the Live Editor
The Retime Timetable task lets you interactively change the row times of a timetable and then resample or aggregate the data in its variables. The task automatically generates MATLAB® code for your live script.
Using this task, you can:
Interpolate data values at new row times.
Aggregate data into time bins (for example, to create a timetable containing quarterly means from monthly data).
Remove rows that have duplicate row times.
Make an irregular timetable into a regular one by specifying a uniform time step or sample rate.
To add the Retime Timetable task to a live script in the MATLAB Editor:
On the Live Editor tab, select Task > Retime Timetable.
In a code block in the script, type a relevant keyword, such as
timetable. Select Retime
Timetable from the suggested command completions.
The example shows how to resample and aggregate data in a timetable by using the Retime Timetable Live Editor task. You also can apply different rules for adjusting timetable variables in different ways.
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);
Add the Retime Timetable task to your live script. Resample the data in
To specify a sample rate of 100 Hz, use the
Selection method menus.
To adjust the data in the
Intensity variable by using linear interpolation, use the
General rule menu.
Load a second timetable from the
bostonTT MAT-file. This timetable has temperature, wind speed, and rainfall measurements for the city of Boston. The measurements are approximately six hours apart but are irregular.
Add a second instance of the Retime Timetable task to your live script. Aggregate the data into daily time bins.
To specify a time step of one day, use the
Selection method menus.
To calculate means of the timetable variables, use the
General rule menu.
To specify a different rule for the
Rain variable, use the
Exceptions menus. Add an exception to interpolate rainfall measurements at the beginning of each day.
Input timetable— Name of timetable
Specify the name from a list of all the nonempty timetables that are in the workspace.
Selection method— Method for specifying row times
Specify the method for calculating new row times of the timetable.
Time step — Length of time between consecutive
regularly spaced row times.
Sample rate — Sample rate or frequency for regularly
spaced row times.
Times from workspace — Workspace variable that is a
duration values. The
times in the vector replace the row times of the timetable. The times need not be
General rule— Rule for adjusting data values
Specify the rule for adjusting data values in the variables of the timetable. The rule applies to all variables unless exceptions are specified. You can resample or aggregate data values to:
Fill gaps with missing data indicators or a constant value.
Interpolate data values to the new row times. For example, resample data measured at 100 Hz to 200 Hz.
Aggregate data into time bins. For example, calculate a daily mean from data values measured at every hour.
Exceptions— Different rule or rules for adjusting specified variables
Click the Add button. Then specify a timetable variable from the drop-down list of variable names that appears.
To adjust the data in the specified variable, select a rule from the drop-down list of rules to the right of the variable name. The rule for the exception can differ from the general rule.
To specify exceptions for other timetable variables, click the + button to the right of the first exception. New drop-down lists of variable names and rules appear below the previous exception.
Behavior changed in R2020b
In R2020b, linear interpolation is the default general rule for adjusting data in the Retime Timetable live editor task. In previous releases, the default general rule was to fill gaps in the output timetable with missing values.
However, even in R2020b the default rule reverts to filling gaps with missing values if either of these conditions is met:
The row times of the input timetable are not sorted.
The input timetable has at least one variable whose data type is not numeric,