Using for loop to create monthly data sets from a timetable with annual data
Ältere Kommentare anzeigen
I have a large timetable datafile that include data for an entire year and 7 columns of additional values. I'm trying to extract the data for each individual month and store the entire rows of data for that month in a seperate table or timetable for further processing within the loop.
Currently I'm able to sort the annual data for a single month by using the code
S = timerange('08/01/1985 00:00:00','09/01/1985 00:00:00');
AugustData = TT(S,:);
I considered something like:
n = 12;
for i = 1:n
monthStart = [num2str(i) '/01/1985 00:00:00'];
monthEnd = [num2str(i+1) '/01/1985 00:00:00'];
S = timerange(monthStart, monthEnd);
AugustData = TT(S,:);
end
However, [num2str(i) '/01/1985 00:00:00'] come out as a char rather than a time. Is there a way to pass a dynamic variable into a time range?
I would appreiciate it if anyone know of any ways to get the data for all 12 month without having to repeat the above code 12 times.
1 Kommentar
dpb
am 10 Mai 2019
"I'm trying to extract the data for each individual month and store the entire rows of data for that month in a seperate table or timetable for further processing within the loop. "
Do NOT do this!!!
Use findgroups and splitapply or varfun or similar techniques instead. Precisely the sort of thing they were made for...
Antworten (1)
Peter Perkins
am 14 Mai 2019
0 Stimmen
+1 to what dpb said, but if you really want to do this
timerange(datetime(1985,i,1),datetime(1985,i+1,1))
should do it.
To use findgroups, you may want to temporarily add a variable to the timetable, e.g.
tt.Month = month(tt.Time)
Kategorien
Mehr zu Dates and Time finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!