www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CombineTimetablesAndSynchronizeTheirDataExample.m
%% Combine Timetables and Synchronize Their Data % You can combine timetables and synchronize their data in a % variety of ways. You can concatenate timetables vertically or % horizontally, but only when they contain the same row times or timetable % variables. Use the |synchronize| function to combine timetables with % different row times and timetable variables. |synchronize| creates a timetable % that contains all variables from all input timetables. It then synchronizes % the data from the input timetables to the row times of the output % timetable. |synchronize| can fill in missing elements of the output % timetable with missing data indicators, with values copied from their % nearest neighbors, or with interpolated values. |synchronize| also can aggregate % timetable data over time bins you specify. %% Concatenate Timetables Vertically % Load timetables from |openPricesSmall| and concatenate them vertically. % The timetables are |opWeek1| and |opWeek2|. They contain opening prices % for some stocks during the first and second weeks of January 2016. load(fullfile(matlabroot,'examples','matlab','openPricesSmall')); %% % Display the two timetables. opWeek1 %% opWeek2 %% % Concatenate the timetables. You can concatenate timetables vertically when % they have the same variables. The row times label the rows and are not contained % in a timetable variable. Note that the row times of a timetable can be % out of order and do not need to be regularly spaced. For example, |op| does not % include days that fall on weekends. A timetable also can contain % duplicate times. |op| contains two rows for |08-Jan-2016 09:00:00|. op = [opWeek2;opWeek1] %% Concatenate Timetables Horizontally % You also can concatenate timetables horizontally. The timetables must % have the same row times and different variables. % % Display the timetable |opOtherStocks|. The timetable has the same % row times as |opWeek1|, but variables for different stocks. opOtherStocks %% % Concatenate |opWeek1| and |opOtherStock|. The output timetable has one % set of row times and the variables from both timetables. op = [opWeek1 opOtherStocks] %% Synchronize Timetables and Indicate Missing Data % Load air quality data and weather measurements from two different % timetables and synchronize them. 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. load(fullfile(matlabroot,'examples','matlab','indoors')); load(fullfile(matlabroot,'examples','matlab','outdoors')); %% % Display the first five lines of each timetable. They contain measurements % of different quantities taken at different times. indoors(1:5,:) %% outdoors(1:5,:) %% % Synchronize the timetables. 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 |Humidity|, |synchronize| renames both variables % and adds both to the output timetable. tt = synchronize(indoors,outdoors); tt(1:5,:) %% Synchronize and Interpolate Data Values % Synchronize the timetables, and fill in missing timetable elements with linear % interpolation. To synchronize on a time vector that includes all times % from both timetables, specify |'union'| for the output times. %% ttLinear = synchronize(indoors,outdoors,'union','linear'); ttLinear(1:5,:) %% Synchronize to Different Times % Synchronize the timetables to an hourly time vector. The input timetables % had irregular row times. The output timetable has regular row times with % one hour as the time step. ttHourly = synchronize(indoors,outdoors,'hourly','linear'); ttHourly(1:5,:) %% % Synchronize the timetables to a time vector that specifies half-hour % intervals. tv = [datetime(2015,11,15):minutes(30):datetime(2015,11,18)]; tv.Format = indoors.Time.Format; ttHalfHour = synchronize(indoors,outdoors,tv,'linear'); ttHalfHour(1:5,:) %% Synchronize and Aggregate Data Values % Synchronize the timetables and calculate the daily means for all % variables in the output timetable. ttDaily = synchronize(indoors,outdoors,'daily','mean'); ttDaily %% % Synchronize the timetables to six-hour time intervals and calculate means % for each interval. tv = [datetime(2015,11,15):hours(6):datetime(2015,11,18)]; tv.Format = indoors.Time.Format; tt6Hours = synchronize(indoors,outdoors,tv,'mean'); tt6Hours(1:5,:)