How to separate and order timetable data by year
Show older comments
data = readtable('USA_SITE_004_final.txt'); %import text file and present as table
data_nan = standardizeMissing(data, -999); %return '-999' values as 'NaN'
hourly_temp_rain_sm = data_nan(:, [1 5 11 26]); %extract key variables
TT = table2timetable(hourly_temp_rain_sm); %form timetable
daily_temp_rain_sm = retime(TT, 'daily', 'mean'); %retime data to obtain daily averages
daily_sm = daily_temp_rain_sm(:,3);
Hi, i have created a timetable and retimed soil moisture data here to obtain daily averages for a period of several years. I now want to be able to obtain a metric for each year and to do this i will need to separate the data into years, keeping the daily averages each year. I hope that makes sense and thank you in advance for any help.
Answers (1)
> I now want to be able to obtain a metric for each year and to do this i will need to separate the data into years, keeping the daily averages each year.
You can use retime with yearly intervals. By storing this in a different variable, you can also keep the daily values.
dt = datetime(1999,1,1) + days(0:1:2000)';
rainfall = rand(numel(dt),1);
TT = timetable(dt, rainfall) % Daily rainfall
yearlyTT = retime(TT,'yearly','mean') % yearly avg
Note that the aggregate method can be a function handle if your metric is not a built-in option. See doc retime for more info.
6 Comments
Star Strider
on 15 Nov 2021
The problem with that approach (that I experimented with, as well as others) is:
‘ i will need to separate the data into years, keeping the daily averages each year.’
That is simply a matter of creating separate table or timetable arrays for each year, a relatively straightforward operation.
.
I would highly recommend to use indexing or to create a simple anonymous function to output a subsection of the timetable by year instead of breaking up timetables. Breaking up the timetable by year would necessitate storing the sub-tables in a cell array which introduces complicated indexing. Surely users should not assign the subtables to different variables since that would either need to be done manually or by using dynamic variable naming which is very poor practice.
For example, if the user wants to temporarily isolate all data from 2001 using an anonymous function,
% Create demo timetable
dt = datetime(1999,1,1) + days(0:1:2000)';
rainfall = rand(numel(dt),1);
TT = timetable(dt, rainfall) % Daily rainfall
% anonymous fcn: enter year as integer
TTbyYearFcn = @(yr)TT(year(TT.dt)==yr,:);
% Get values from 2001
TTbyYearFcn(2001)
% Example of extracting rainfall from 2001
TTbyYearFcn(2001).rainfall
Adam Danz
on 15 Nov 2021
Another approach would be to create a 3D array of the data where one dimention defines dates by year but that get a little tickly with leapdays, but still achievable.
Joe Wheeler
on 16 Nov 2021
Joe Wheeler
on 16 Nov 2021
Edited: Adam Danz
on 17 Nov 2021
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!