This example shows how to create a timetable, combine timetables, and adjust the data from multiple timetables to a common time vector. The common time vector can contain the times from either or both timetables, or it can be an entirely new time vector that you specify. The example shows how to compute and display a daily mean for weather measurements contained in different timetables.
A timetable is a type of table that associates a time with each row. A timetable can store column-oriented data variables that have different data types and sizes, so long as each variable has the same number of rows. In addition, timetables provide time-specific functions to combine, subscript into, and adjust their data.
Import Timetables from Files
Load air quality data and weather measurements into two different timetables. The dates of the measurements range from November 15, 2015, to November 19, 2015. The air quality data come from a sensor inside a building, while the weather measurements come from sensors outside.
Read the air quality data from a table with the
readtimetable function. The output is a timetable.
indoors = readtimetable('indoors.csv');
You also can create a timetable from an M-by-N array with the
array2timetable function, or from workspace variables with the
Display the first five rows of
indoors. Each row of the timetable has a time that labels that row of data.
ans=5×2 timetable Time Humidity AirQuality ___________________ ________ __________ 2015-11-15 00:00:24 36 80 2015-11-15 01:13:35 36 80 2015-11-15 02:26:47 37 79 2015-11-15 03:39:59 37 82 2015-11-15 04:53:11 36 80
Load the timetable with weather measurements. Display the first five rows of
load outdoors outdoors(1:5,:)
ans=5×3 timetable Time Humidity TemperatureF PressureHg ___________________ ________ ____________ __________ 2015-11-15 00:00:24 49 51.3 29.61 2015-11-15 01:30:24 48.9 51.5 29.61 2015-11-15 03:00:24 48.9 51.5 29.61 2015-11-15 04:30:24 48.8 51.5 29.61 2015-11-15 06:00:24 48.7 51.5 29.6
outdoors, contain different measurements taken inside and outside a building at different times. Combine all the data into one timetable with the
tt = synchronize(indoors,outdoors); tt(1:5,:)
ans=5×5 timetable Time Humidity_indoors AirQuality Humidity_outdoors TemperatureF PressureHg ___________________ ________________ __________ _________________ ____________ __________ 2015-11-15 00:00:24 36 80 49 51.3 29.61 2015-11-15 01:13:35 36 80 NaN NaN NaN 2015-11-15 01:30:24 NaN NaN 48.9 51.5 29.61 2015-11-15 02:26:47 37 79 NaN NaN NaN 2015-11-15 03:00:24 NaN NaN 48.9 51.5 29.61
The output timetable,
tt contains all the times from both timetables.
synchronize puts a missing data indicator where there are no data values to place in
tt. When both input timetables have a variable with the same name, such as
synchronize renames both variables and adds both to the output timetable.
Synchronize the timetables again, and this time fill in missing data values with linear interpolation.
ttLinear = synchronize(indoors,outdoors,'union','linear'); ttLinear(1:5,:)
ans=5×5 timetable Time Humidity_indoors AirQuality Humidity_outdoors TemperatureF PressureHg ___________________ ________________ __________ _________________ ____________ __________ 2015-11-15 00:00:24 36 80 49 51.3 29.61 2015-11-15 01:13:35 36 80 48.919 51.463 29.61 2015-11-15 01:30:24 36.23 79.77 48.9 51.5 29.61 2015-11-15 02:26:47 37 79 48.9 51.5 29.61 2015-11-15 03:00:24 37 80.378 48.9 51.5 29.61
Adjust Data in One Timetable
You also can adjust the data in a single timetable to a new time vector. Calculate the means of the variables in
ttLinear over six-hour intervals with the
retime function. If any rows have
NaN values after you adjust the data, remove them with the
tv = [datetime(2015,11,15):hours(6):datetime(2015,11,18)]; ttHourly = retime(ttLinear,tv,'mean'); ttHourly = rmmissing(ttHourly);
Plot Timetable Data
Normalize the data in
ttHourly to the mean for each variable in the timetable. Plot the mean daily values of these measurements. You can use the
Variables property of a timetable to access the variables.
ttHourly.Variables returns the same variables as
ttMeanVars = ttHourly.Variables./mean(ttHourly.Variables); plot(ttHourly.Time,ttMeanVars); legend(ttHourly.Properties.VariableNames,'Interpreter','none'); xlabel('Time'); ylabel('Normalized Weather Measurements'); title('Mean Daily Weather Trends');
- Resample and Aggregate Data in Timetable
- Combine Timetables and Synchronize Their Data
- Retime and Synchronize Timetable Variables Using Different Methods
- Select Times in Timetable
- Clean Timetable with Missing, Duplicate, or Nonuniform Times
- Data Cleaning and Calculations in Tables
- Grouped Calculations in Tables and Timetables