MATLAB: Devide a table in several tables by criteria

devide tableMATLABsplit tabletimetable

Guys, Hi again,
I didn't want to use my first thread, because this question is a bit different.
I got a timetable which I want to split in several smaller tables using a criteria from one column in the timetable. If you would plot the time and the column Volt you would get several curves. I want to split the data per curve in a single table.
In the timetable Tdata in the column OnOff I got values only 0 or 1.
1 means current flows, 0 no current. So I want that the script goes through the column OnOff and search for the first 1, copy all the rows until the next 0 comes and write an table. And continue this to the end of the column OnOff.
My question is, is something like this possible? And if yes, could you help me with the loop, somehow I have no idea how to solve it.
Many Thanks!

Best Answer

You can do this in several ways, but honestly the most straight-forward is to write a loop, as shown below. The real question is, why do you want to do this? I'm genuinely asking, because there are tools that let you do things like hourly means without splitting the data up. I guess your answer would be, "I want to plot things separately". Fair enough: you can trick rowfun into doing that but it's a little tricky. Are there other things you want to do on the separate timetables?
Anyways, my solution (which could certainly be tightened up but is at least clear):
>> tt = timetable(rand(15,1),[1;1;1;0;0;1;1;1;1;0;1;1;1;1;1], ...
'RowTimes',datetime(2019,6,19,0,0,0:14), ...
'VariableNames',{'X' 'OnOff'});
>> tt.Group = 1 + [0; cumsum(diff([tt.OnOff])~=0)]
tt =
15×3 timetable
Time X OnOff Group
____________________ ________ _____ _____
19-Jun-2019 00:00:00 0.69989 1 1
19-Jun-2019 00:00:01 0.63853 1 1
19-Jun-2019 00:00:02 0.033604 1 1
19-Jun-2019 00:00:03 0.068806 0 2
19-Jun-2019 00:00:04 0.3196 0 2
19-Jun-2019 00:00:05 0.53086 1 3
19-Jun-2019 00:00:06 0.65445 1 3
19-Jun-2019 00:00:07 0.40762 1 3
19-Jun-2019 00:00:08 0.81998 1 3
19-Jun-2019 00:00:09 0.71836 0 4
19-Jun-2019 00:00:10 0.96865 1 5
19-Jun-2019 00:00:11 0.53133 1 5
19-Jun-2019 00:00:12 0.32515 1 5
19-Jun-2019 00:00:13 0.10563 1 5
19-Jun-2019 00:00:14 0.61096 1 5
>> c = cell(numel(unique(tt.Group)),1);
>> for i = 1:length(c), c{i} = tt(tt.Group == i,1:2); end
>> c{:}
ans =
3×2 timetable
Time X OnOff
____________________ ________ _____
19-Jun-2019 00:00:00 0.69989 1
19-Jun-2019 00:00:01 0.63853 1
19-Jun-2019 00:00:02 0.033604 1
ans =
2×2 timetable
Time X OnOff
____________________ ________ _____
19-Jun-2019 00:00:03 0.068806 0
19-Jun-2019 00:00:04 0.3196 0
ans =
4×2 timetable
Time X OnOff
____________________ _______ _____
19-Jun-2019 00:00:05 0.53086 1
19-Jun-2019 00:00:06 0.65445 1
19-Jun-2019 00:00:07 0.40762 1
19-Jun-2019 00:00:08 0.81998 1
ans =
1×2 timetable
Time X OnOff
____________________ _______ _____
19-Jun-2019 00:00:09 0.71836 0
ans =
5×2 timetable
Time X OnOff
____________________ _______ _____
19-Jun-2019 00:00:10 0.96865 1
19-Jun-2019 00:00:11 0.53133 1
19-Jun-2019 00:00:12 0.32515 1
19-Jun-2019 00:00:13 0.10563 1
19-Jun-2019 00:00:14 0.61096 1