Divide timeseries to monthly ones
12 ビュー (過去 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
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!