Clear Filters
Clear Filters

Time subscripting, selection of weekdays and weekends from a timetable

3 views (last 30 days)
Hi guys.
I have had this problem for a while now. I have a timetable consisting of timeseries yearly data resolved at 30 minutes. I am writing a script which should split the data into the summer and winter, weekdays summer and winter, and weekend summer and winter. The code I have written is below. It does work but the explarotory figures reveal that there is a problem with the way this data is split, or suscripted internally. Ideally, I should be able to locate the maximum demand for winter or summer; and the weekdays and the weekedn should be subset data of the summer and winter data sets. Anyone able to debug this code for me will be appreciated.
%% convert the timeseries for you to derive the seasons easily
SeasonTT =table2timetable(SA_RXM); % generate the whole year time table
%%
Res=30;
D_factor = (24*60/Res);
%% Analyze the summer
Summer = SeasonTT(ismember(month(SeasonTT.Date), [1,2,11,12]), :); % Defined for 1,2,11 and 12
Wkday_summer =SeasonTT(~isweekend(Summer.Date), :); % obtains your weekday
Wkend_summer=SeasonTT(isweekend(Summer.Date), :); % obtains your weekend
%% Convert the summer season into arrays
No_Days = floor(size(Summer,1)/D_factor);
x=No_Days*D_factor;
% Extract the summer array
SeasonArrayT=timetable2table(Summer(1:x,:));
Summer_A=table2array(SeasonArrayT(:,2:end));
% Extract the summer weekend and weekday from data
WkendT=timetable2table(Wkend_summer);
WkdayT=timetable2table(Wkday_summer);
Cust_No= size(Summer_A,2);
No_Days_1 = floor(size(WkdayT,1)/D_factor);
No_Days_2 = floor(size(WkendT,1)/D_factor);
xd=No_Days_1*D_factor;
xw=No_Days_2*D_factor;
Wkday_S=table2array(WkdayT(1:xd,2:end));
Wkend_S=table2array(WkendT(1:xw,2:end));
clearvars SeasonArrayT WkdayT WkendT;
%% Repeat the above process for winter data
Winter = SeasonTT(ismember(month(SeasonTT.Date), [5,6,7]), :);
Wkday_winter =SeasonTT(~isweekend(Winter.Date), :); % obtains your weekday
Wkend_winter=SeasonTT(isweekend(Winter.Date), :); % obtains your weekend
%% Convert the winter season into arrays
No_Days = floor(size(Winter,1)/D_factor);
x=No_Days*D_factor;
%Extract the summer array
SeasonArrayT=timetable2table(Winter(1:x,:));
Winter_A=table2array(SeasonArrayT(:,2:end));
%Extract the summer weekend and weekday from data
WkendT=timetable2table(Wkend_winter);
WkdayT=timetable2table(Wkday_winter);
No_Days_1 = floor(size(WkdayT,1)/D_factor);
No_Days_2 = floor(size(WkendT,1)/D_factor);
xd=No_Days_1*D_factor;
xw=No_Days_2*D_factor;
Wkday_W=table2array(WkdayT(1:xd,2:end));
Wkend_W=table2array(WkendT(1:xw,2:end));
clearvars SeasonArrayT WkdayT WkendT;
%% Extract the season periods and obtain day of max demand
% For all the columns
H_intv = 48; % specifies the number of intervals in one hour
DataS={Summer_A,Wkday_S,Wkend_S};
DataW={Winter_A,Wkday_W, Wkend_W};
for i=1:size(DataS,2)
%xpts=(1:size(DataC{i},1));
if (size(DataS{i},2))==1
[mx,id_mx]=max(DataS{i});
max_day =ceil(id_mx/H_intv);
%i=i+1;
elseif (size(DataS{i},2))>1
mx_demand=max(DataS{i}.').';
[mx,id_mx]=max(mxDemand);
max_day=ceil(id_mx/H_intv);
end
Data_smx(:,i)= DataS{i}((H_intv*(max_day-1):H_intv*max_day),:); % For comparing the extracted max day data
end
for i=1:size(DataW,2)
%xpts=(1:size(DataC{i},1));
if (size(DataW{i},2))==1
[mx,id_mx]=max(DataW{i});
max_day =ceil(id_mx/H_intv);
%i=i+1;
elseif (size(DataW{i},2))>1
mx_demand=max(DataW{i}.').';
[mx,id_mx]=max(mxDemand);
max_day=ceil(id_mx/H_intv);
end
Data_wmx(:,i)= DataW{i}((H_intv*(max_day-1):H_intv*max_day),:);
end
%%
subplot(2,3,1)
plot(DataS{1})
title('Whole Summer Profile');
subplot(2,3,2)
plot(DataS{2})
title('Weekday Summer Profile');
subplot(2,3,3)
plot(DataS{3})
title('Weekend Summer Profile');
subplot(2,3,4)
plot(DataW{1})
title('Whole winter Profile');
subplot(2,3,5)
plot(DataW{2})
title('Weekday Winter Profile');
subplot(2,3,6)
plot(DataW{3})
title('Weekend Winter Profile');
The obtained plot is shown in the attached .png and matlab figure - and the data used is attached as well. As can be seen, splitting the summer data into weekdays and weekeds, the split data does not have the min point for summer season, while the winter weekdays and weekends data are not even a subset of the whole winter data.
Thank you.

Answers (1)

Nihal
Nihal on 29 Sep 2023
Hi Lewis,
I understand that you are encountering challenges as the plots generated by your code did not accurately match with the expected results.
After analysing the above code, I found the problem is with the way you are extracting data using logical indexing.
Wkday_summer =SeasonTT(~isweekend(Summer.Date), :); % wrong
Wkday_summer =Summer(~isweekend(Summer.Date), :); % correct
In the above line you have extracted logical indexes according to 'Summer' but you are passing it to 'SeasonTT'. So, this will give you data from 'SeasonTT' which is wrong in this case.
Below is the short example explaining same:
naturalNumber = [1:20]
twoDigits = naturalNumber(naturalNumber>10)
greaterTwoDigitsIndex = twoDigits>15 % I got logical indexes according to twoDigits array
greaterTwoDigits1 = naturalNumber(greaterTwoDigitsIndex) % wrong output
greaterTwoDigits2 = twoDigits(greaterTwoDigitsIndex) % correct output
To generate correct plots, extract values from 'Summer' not 'SeasonTT' and do same for 'Winter' data.
I hope this information resolves your query.

Categories

Find more on Timetables in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!