Divide timeseries to monthly ones
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ancalagon8
il 15 Gen 2023
Modificato: Ancalagon8
il 6 Gen 2025 alle 21:49
For a specific year I have created an hourly timetable using retime.
How can I divide into monthly ?
0 Commenti
Risposta accettata
Star Strider
il 15 Gen 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')
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 Commenti
Star Strider
il 24 Gen 2023
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!’
.
Più risposte (1)
Christopher McCausland
il 15 Gen 2023
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 Commento
Christopher McCausland
il 15 Gen 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
Vedere anche
Categorie
Scopri di più su Tables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!