Divide timeseries to monthly ones
13 次查看(过去 30 天)
显示 更早的评论
For a specific year I have created an hourly timetable using retime.
How can I divide into monthly ?
0 个评论
采纳的回答
Star Strider
2023-1-15
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')
for k = 1:12
MMidx = month(VarPerHour.date_time) == k;
VarPerHourMonth{k,:} = VarPerHour(MMidx,:);
end
VarPerHourMonth
VarPerHourMonth{1}(1:5,:)
VarPerHourMonth{12}(1:5,:)
This uses an existing timetable. It should work with the one you are currently using as well.
.
10 个评论
Star Strider
2023-1-24
Let’s do that experiment —
LD = load(websave('dataset','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1255052/dataset.mat'));
T = LD.TT1;
VarPerHour = retime(T, 'hourly', 'sum')
for k = 1:12
MMidx = month(VarPerHour.date_time) == k;
VarPerHourMonth{k,:} = VarPerHour(MMidx,:);
end
for k = 1:12
TTTemp = VarPerHourMonth{k}; % Create Temporary 'timetable'
Hours = hour(TTTemp.date_time); % Create 'Hours' Variable
[y,m,d] = ymd(TTTemp.date_time); % Begin To Create 'Date' Variable
Date = datetime(y,m,d); % Finish Creating 'Date' Variable
TTTemp = addvars(TTTemp, Date, Hours,'Before','Temperature'); % Add 'Hours' & 'Date' Variables
TTTemp.Properties.VariableNames(1:2) = {'Date','Hours'}; % Name 'Hours' & 'Date' Variables
TTTempT = timetable2table(TTTemp); % Convert To 'table'
VarPerHourMonthT{k,:} = unstack(TTTempT(:,2:end),'Temperature','Hours', 'VariableNamingRule','preserve'); % Unstack & Write To Cell Array
MMM{k,:} = month(TTTemp.date_time(1,:),'shortname');
end
Filename = 'RainPerHourMonth.xlsx';
for k = 1:12
writetable(VarPerHourMonthT{k}, Filename, 'Sheet',string(MMM{k}))
end
T6 = readtable(Filename, 'Sheet','Jun', 'VariableNamingRule','preserve') % Check 6
For some reason, writetable doesn’t like the cell array (even though indexing into it should produce a character array), however it accepts the string argument. Referring to it by name in the readtable test works. (The ‘MMM’ cell array didn’t initially appear in this version of my code, so I added it in the loop.)
So, an emphatic ‘Yes!’
.
更多回答(1 个)
Christopher McCausland
2023-1-15
Hi Ancalogon,
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
1 个评论
Christopher McCausland
2023-1-15
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!