Divide annual timeseries to monthly ones

8 views (last 30 days)
For a specific year I have created an hourly timetable using retime:
VarPerHour = retime(T, 'hourly', 'sum');
How can I divide VarPerHour into 12 monthly timetables?

Accepted Answer

Star Strider
Star Strider on 15 Jan 2023
A for loop is the easiest way to do this —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
T = LD.TT1;
VarPerHour = retime(T, 'hourly', 'sum')
VarPerHour = 8760×1 timetable
date_time Temperature __________________ ___________ 01-Jan-19 00:00:00 588 01-Jan-19 01:00:00 608.11 01-Jan-19 02:00:00 608.25 01-Jan-19 03:00:00 608.33 01-Jan-19 04:00:00 608.25 01-Jan-19 05:00:00 608.4 01-Jan-19 06:00:00 608.59 01-Jan-19 07:00:00 608.9 01-Jan-19 08:00:00 609.32 01-Jan-19 09:00:00 599.51 01-Jan-19 10:00:00 609.61 01-Jan-19 11:00:00 609.51 01-Jan-19 12:00:00 609.39 01-Jan-19 13:00:00 609.44 01-Jan-19 14:00:00 609.58 01-Jan-19 15:00:00 609.83
for k = 1:12
MMidx = month(VarPerHour.date_time) == k;
VarPerHourMonth{k,:} = VarPerHour(MMidx,:);
end
VarPerHourMonth
VarPerHourMonth = 12×1 cell array
{744×1 timetable} {672×1 timetable} {744×1 timetable} {720×1 timetable} {744×1 timetable} {720×1 timetable} {744×1 timetable} {744×1 timetable} {720×1 timetable} {744×1 timetable} {720×1 timetable} {744×1 timetable}
VarPerHourMonth{1}(1:5,:)
ans = 5×1 timetable
date_time Temperature __________________ ___________ 01-Jan-19 00:00:00 588 01-Jan-19 01:00:00 608.11 01-Jan-19 02:00:00 608.25 01-Jan-19 03:00:00 608.33 01-Jan-19 04:00:00 608.25
VarPerHourMonth{12}(1:5,:)
ans = 5×1 timetable
date_time Temperature __________________ ___________ 01-Dec-19 00:00:00 590.64 01-Dec-19 01:00:00 193.5 01-Dec-19 02:00:00 0 01-Dec-19 03:00:00 0 01-Dec-19 04:00:00 0
This uses an existing timetable. It should work with the one you are currently using as well.
.
  15 Comments
Ancalagon8
Ancalagon8 on 24 Jan 2023
Worked perfect (as always)! Thanks!!

Sign in to comment.

More Answers (1)

Christopher McCausland
Christopher McCausland on 15 Jan 2023
Hi Ancalogon,
Having a look at the documentation here; you can just replace hourly with any timestep such as:
Time Step
'yearly'
'quarterly'
'monthly'
'weekly'
'daily'
'hourly'
'minutely'
'secondly'
Have you tried;
VarPermonth = retime(T, 'monthly', 'sum');
Let me know if this is what you are looking for, if not please provide a snippit of the data and the expected output.
Christopher
  2 Comments
Ancalagon8
Ancalagon8 on 15 Jan 2023
Thanks for your answer @Christopher McCausland.
VarPermonth = retime(T, 'monthly', 'sum');
returns me only one value per month (12X1 timetable).
VarPerHour = retime(T, 'hourly', 'sum') is a 8760X1 timetable (365 days X 24 hours).
I need to split VarPerHour per month but keep all values.
Christopher McCausland
Christopher McCausland on 15 Jan 2023
Hi Ancalagon,
I get what you want now.
What you really need to do is filter ValPerHour by months, heres an example of how to do so:
And also an ealier suggestion from Walter;
I hope this helps!
Christopher

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!