Using for loop to create monthly data sets from a timetable with annual data
8 ビュー (過去 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
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
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)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!