clc;
clear;
data = readtable('A_minute.xlsx');
DaT = datenum(data.Var1);
t = datetime(DaT, 'ConvertFrom','datenum');
TimeofDay = timeofday(t);
Now I made the first change here. I ceil the time to hours i.e. if time is between 0hrs and 1 hrs it is 1hrs, if bw 1hrs and 2hrs it is 2hrs.
TimeofDay = ceil(TimeofDay,'hours');
Now i apply unique and save 3rd argument in pos. what we have in 3rd argument is the unique value position by index
For example
[a,~,ic] = unique([4 5 5 4])
then the unqiue values are
a =
4 5
and there position in input matrix is
ic =
1
2
2
1
i.e. 1st unique value is 1 and 2nd unique value is 2. so where ever 1 exist means the 1st value was on this position and wherever 2 exist means 2nd unique value was here and so on (if there are others)
[UniqueTimeSteps,~,pos] = unique(TimeofDay);
We get 25 unique values because we have
00:00:00
01:00:00
.
.
.
24:00:00
So in pos wherever 1 exist means 00:00:00 and so on till 25.
Now it is your choice, either add 0:00:00 to 1:00:00 or to 24:00:00. I am adding it to 24:00:00
Remove the 1st value from UniqueTimeSteps
UniqueTimeSteps = UniqueTimeSteps(2:end);
Subtract 1 from pos. so that 01:00:00 corresponds to 1, 02:00:00 corresponds to 2 and so on
since we subtracted 1 from pos, so 00:00:00 pos changes from 1 to zero. Replace it with 24.
AptA = data(:,4);
AptA = table2array(AptA);
Consumption_Weekday = zeros(1,length(UniqueTimeSteps));
Consumption_Weekend = zeros(1,length(UniqueTimeSteps));
wd = weekday(t) > 1 & weekday(t) < 7;
Run for loops for 24 iterations (hours) which are in UniqueTImeSteps
for i = 1:length(UniqueTimeSteps)
suppose i =1, pos==1 is making all index equal to 1 true (for logical indexing). similarly for i=2,3,..24
idx = (pos == i & wd);
Consumption_Weekday(i) = mean(AptA(idx));
idx = (pos == i & (~wd));
Consumption_Weekend(i) = mean(AptA(idx));
end
MinuteValue= UniqueTimeSteps;
figureA = figure;
plot(MinuteValue,Consumption_Weekday,'-b');
hold on
plot(MinuteValue,Consumption_Weekend,'-r');
xlabel('Time'), ylabel('Aggregated Electric Demand (kW)'), ...
title('Unit A - Aggregated HVAC Electric Demand (kW)'), ...
legend('Weekdays','Weekends')
I hope this is what you want. Working code is attached.