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

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 comentario

"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...

Iniciar sesión para comentar.

Respuestas (1)

+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)

Categorías

Preguntada:

el 10 de Mayo de 2019

Respondida:

el 14 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by