MATLAB: Create subsets of data within a timetable

for loop

I have a timetable (TT) and I need to create data subsets based on time duration vectors. The code below does what I need but I ended up with 6 timetables in my workspace, which is a problem for me because I then need to do a for loop to conduct several operations on each subset and I don't know how to iterate through different timetables. I would be so grateful if someone could tell me if there is a way to have these 6 subsets all in a single timetable so I can then have a loop iterating through them? Alternatively, how can I loop through my different timetables, considering they have different names (t1, t2, etc)? Thank you so much in advance for your help!
t1 = TT(timerange(hours(00), hours(04)), :);
t2 = TT(timerange(hours(04), hours(08)), :);
t3 = TT(timerange(hours(08), hours(12)), :);
t4 = TT(timerange(hours(12), hours(16)), :);
t5 = TT(timerange(hours(16), hours(20)), :);
t6 = TT(timerange(hours(20), hours(24)), :);

Best Answer

As you've discovered creating six individually named timetables is not a good idea. As a rule, as soon as you start numbering variables, you're doing something wrong.
starttimes = 0:4:20;
endtimes = 4:4:24;
tts = arrayfun(@(s, e) TT(timerange(hours(s), hours(e)), :), starttimes, endtimes, 'UniformOutput', false);
will create a cell array with your 6 timetables. It's then trivial to iterate over the element of the cell array:
for tidx = 1:numel(tts)
tts{idx}
%do something with tts{tidx}, a single timetable
end
Alternatively, instead of looping over the timetables in the cell array, you could indeed just merge them into one by concatenating them vertically:
filteredtt = vertcat(tts{:})