www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CreateTimetablesExample.m
%% Create Timetables % 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 |readtable| function. % Then convert it from a table to a timetable with the |table2timetable| % function. The |readtable| function returns a table only, not a timetable. indoors = readtable(fullfile(matlabroot,'examples','matlab','indoors.csv')); indoors = table2timetable(indoors); %% % You also can create a timetable from an M-by-N array with the % |array2timetable| function, or from workspace variables with the % |timetable| function. % % Display the first five rows of |indoors|. Each row of the timetable has a % time that labels that row of data. indoors(1:5,:) %% % Load the timetable with weather measurements. Display the first five rows % of |outdoors|. load(fullfile(matlabroot,'examples','matlab','outdoors')); outdoors(1:5,:) %% Synchronize Timetables % The timetables, |indoors| and |outdoors|, contain different measurements % taken inside and outside a building at different times. Combine % all the data into one timetable with the |synchronize| function. tt = synchronize(indoors,outdoors); tt(1:5,:) %% % 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. % % Synchronize the timetables again, and this time fill in missing data values with linear % interpolation. ttLinear = synchronize(indoors,outdoors,'union','linear'); ttLinear(1:5,:) %% 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 |rmmissing| function. 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 |ttHourly{:,:}|. 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');