# Grouping minute data by day and assigning to a variable

2 views (last 30 days)
Jaewon Lee on 22 Dec 2021
Edited: DGM on 22 Dec 2021
Hey guys, I have an electricity load data in the following format
and I would like to group them by each day, so that I would have the data in the following format
I was trying to use a while loop, but I am pretty bad at coding.. Is there anyone that could help me out with this issue?

DGM on 22 Dec 2021
Edited: DGM on 22 Dec 2021
I never really do much with datetime objects, so I'm sure that there are better ways. This is at least a start. This should group the data by day. Note that this does not guarantee that the minutewise samples line up. In this example, two of the days are partial. How such a case is handled (e.g. padding) is up to you.
t1 = datetime(2020,11,1,8,0,0);
t2 = datetime(2020,11,3,8,0,0);
t = (t1:minutes(60):t2).';
data = (1:numel(t)).';
[ty tm td] = ymd(t);
tymd = [ty tm td];
tymdu = unique(tymd,'rows')
tymdu = 3×3
2020 11 1 2020 11 2 2020 11 3
ndays = size(tymdu,1);
outdata = cell(ndays,1);
for d = 1:ndays
end
celldisp(outdata)
outdata{1} = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 outdata{2} = 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 outdata{3} = 41 42 43 44 45 46 47 48 49
Using the cell array is necessary since there's no guarantee that the sample vectors are the same size. If padding is performed, then a regular array can be used instead:
t1 = datetime(2020,11,1,8,0,0);
t2 = datetime(2020,11,3,8,0,0);
t = (t1:minutes(60):t2).';
data = (1:numel(t)).';
[ty tm td] = ymd(t);
tymd = [ty tm td];
tymdu = unique(tymd,'rows'); % unique days
[th tmn ts] = hms(t);
thms = [th tmn ts];
thmsu = unique(thms,'rows'); % unique sample times
ndays = size(tymdu,1);
nsampperday = size(thmsu,1);
outdata = NaN(ndays,nsampperday);
for d = 1:ndays
end
outdata
outdata = 3×24
NaN NaN NaN NaN NaN NaN NaN NaN 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
daylabels = datestr([tymdu zeros(size(tymdu))],'yyyy-mm-dd')
daylabels = 3×10 char array
'2020-11-01' '2020-11-02' '2020-11-03'
sampletimelabels = datestr([[2000 1 1].*ones(size(thmsu)) thmsu],'HH:MM:SS')
sampletimelabels = 24×8 char array
'00:00:00' '01:00:00' '02:00:00' '03:00:00' '04:00:00' '05:00:00' '06:00:00' '07:00:00' '08:00:00' '09:00:00' '10:00:00' '11:00:00' '12:00:00' '13:00:00' '14:00:00' '15:00:00' '16:00:00' '17:00:00' '18:00:00' '19:00:00' '20:00:00' '21:00:00' '22:00:00' '23:00:00'