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

조회 수: 8 (최근 30일)
Michelle Yan
Michelle Yan 2019년 5월 10일
답변: Peter Perkins 2019년 5월 14일
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