Using for loop to create monthly data sets from a timetable with annual data

9 次查看(过去 30 天)
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 个评论
dpb
dpb 2019-5-10
"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...

请先登录,再进行评论。

回答(1 个)

Peter Perkins
Peter Perkins 2019-5-14
+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)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by