Separate time series data in Individual Months
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ben Whitby
le 18 Mar 2023
Commenté : Star Strider
le 20 Mar 2023
Hi ALL,
I have a large time series of data sampled every 5 minutes starting '01-Oct-2021 00:00:00' and ending '28-Feb-2023 23:54:59'. There are two columns one is datetime(dd:MM:yyyy:HH:mm:ss) and the other is a variable called Volts A-N. It goes as follows
Time Volts A-N
'01-Oct-2021 04:59:59' 120
'01-Oct-2021 05:04:59' 130
'01-Oct-2021 05:09:59' 123
'01-Oct-2021 05:14:59' 145
'01-Oct-2021 05:19:59' 123
'01-Oct-2021 05:24:59' 133
'01-Oct-2021 05:29:59' 132
'01-Oct-2021 05:34:59' 134
'01-Oct-2021 05:39:59' 134
'01-Oct-2021 05:44:59' 132
'01-Oct-2021 05:49:59' 123
etc until
'28-Feb-2023 23:54:59' 157
Is there an easy way to split the data into individual months by finding the index of the data value corresponding to the start and end of each month? I want to be able to calculate the mean and standard deviation of the data in each individual month. Obviously if the months were all the same lengths I could simply use reshape() function but they are not. Any help would be appreciated.
Thanks
2 commentaires
Réponse acceptée
Star Strider
le 18 Mar 2023
It’s not necessary to determine the beginning and dne of each month to separate them.
This approach uses the ymd function with findgroups to separate the dates by month and year, and then uses accumarray to do the ‘heavy lifting’ to separate the resulting table by month and year.
Try something like this —
Start = datetime('01-Oct-2021 00:00:00');
End = datetime('28-Feb-2023 23:54:59');
DTV = (Start : minutes(5) : End).'; % Date Time Vector
DateVolts = table(DTV, randi([120 160],size(DTV)), 'VariableNames',{'Time','Volts A-H'}) % Create Data Table
[y,m,d] = ymd(DateVolts.Time); % Return Year, Month, & Day
[G,IDm,IDy] = findgroups(y,m); % Create Group Reference
MonthsByYear = accumarray(G,(1:size(DateVolts,1)).', [], @(x){DateVolts(x,:)}) % Segment By Month & Year
MonthsByYear{1}([1 end],:) % First Month In Series
MonthsByYear{17}([1 end],:) % Last Month In Series
There are other functions as well that can do this. I prefer accumarray because I’m used to it.
.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!