Date and times logic
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Adnan Jayyousi
am 24 Jun. 2022
Beantwortet: Steven Lord
am 25 Jun. 2022
Hello,
I am trying to make array that contains 8760 Hours, starting from 1/1/2021 ending in 31/12/2021,
in the second column in the same array I want to fill in the corresponding electricty tariff at that certain time.
the tariff values may vary according to the month/days/hours/holidays along the year.
My intention is to make a logic using while loop (8760 loops) using index "i".
I am having trouble to find easy logic that can be applied on the "DateNtime" array in order to fill the tariff.
I will give an example for one scenario in words :
If Season is 1 (one of months Jan/Feb/Dec) & the hour is between 8:00 and 13:00 & the day is normal week day (Sunday till thursday), the tariff wil be 0.7170 (put into column 2 -- > DateNtime(x,2) = 0.7170
any suggestions ?
%% Define date&time arrays :
t1 = datetime(2021,1,1,0,0,0);
t2 = datetime(2021,12,31,23,0,0);
DateNtime = (t1:hours(1):t2)'
%% Create Holidays arrays :
ht1 = datetime(2021,3,27,0,0,0);
ht2 = datetime(2021,3,28,23,0,0);
HT1 = (ht1:hours(1):ht2)'
%%
ht3 = datetime(2021,4,2,0,0,0);
ht4 = datetime(2021,4,3,23,0,0);
HT2 = (ht3:hours(1):ht4)'
%%
ht5 = datetime(2021,4,14,0,0,0);
ht6 = datetime(2021,4,15,23,0,0);
HT3 = (ht5:hours(1):ht6)'
%%
ht7 = datetime(2021,4,14,0,0,0);
ht8 = datetime(2021,4,15,23,0,0);
HT4 = (ht7:hours(1):ht8)'
%%
ht9 = datetime(2021,5,16,0,0,0);
ht10 = datetime(2021,5,17,23,0,0);
HT5 = (ht9:hours(1):ht10)'
%%
ht11 = datetime(2021,9,6,0,0,0);
ht12 = datetime(2021,9,7,23,0,0);
HT6 = (ht11:hours(1):ht12)'
%%
ht13 = datetime(2021,9,15,0,0,0);
ht14 = datetime(2021,9,16,23,0,0);
HT7 = (ht13:hours(1):ht14)'
%%
ht15 = datetime(2021,9,20,0,0,0);
ht16 = datetime(2021,9,20,23,0,0);
HT8 = (ht15:hours(1):ht16)'
%%
ht17 = datetime(2021,9,27,0,0,0);
ht18 = datetime(2021,9,27,23,0,0);
HT9 = (ht17:hours(1):ht18)'
%%
%% Holidays Array :
Hdays = [HT1 ; HT2 ; HT3 ; HT4 ; HT5 ; HT6 ; HT7 ; HT8 ; HT9];
%% Start Loop for filling tariffs :
i = 1:1:8760;
while ( i < 8760 )
????
end
%% Create tariff table according to seasons (season 1 (Dec,Feb,Jan), Season 2 (March,april,may,october,november),season 3(june,july,august,september) :
Season1Tariffs = [0.7170,0.2307];
array2table(Season1Tariffs, 'VariableNames', {'OnPeak','OffPeak'});
Season2Tariffs = [0.2578,0.2243];
array2table(Season2Tariffs, 'VariableNames', {'OnPeak','OffPeak'});
Season3Tariffs = [1.0789,0.2701];
array2table(Season3Tariffs, 'VariableNames', {'OnPeak','OffPeak'});
Thanks !
2 Kommentare
Dyuman Joshi
am 25 Jun. 2022
Note - Your Hdays array doesn't have 8760 elements.
Use a for loop -
%random date and time array
dt=[datetime(2021,3,29,10,20,30) datetime(2021,6,15,12,10,8) datetime(2021,12,1,13,17,19)]
DateNtime=zeros(numel(dt),2); %preallocating
for i=1:numel(dt)
if hour(dt(i))>=8 & hour(dt(i))<13 & ismember(day(dt(i),'dayofweek'),[1 2 3 4 5])
switch month(dt(i))
case {12,1,2}
DateNtime(i,2)=0.7170;
case {3,4,5,10,11}
DateNtime(i,2)=0.2578;
case {6,7,8,9}
DateNtime(i,2)=1.0789;
end
end
end
DateNtime
Akzeptierte Antwort
Steven Lord
am 25 Jun. 2022
Let's say you had some sample dates with time components.
rng default
d = datetime(2022, randi(12, 10, 1), randi(31, 10, 1), randi(24, 10, 1), randi([0 59], 10, 1), 0)
And you had a vector of holidays with the time component set to midnight of that day.
holidays = dateshift(d([2 7 4]), 'start', 'day')
We can see which of the elements in d fall on the same days as those in holidays.
isaHoliday = ismember(day(d, 'dayofyear'), day(holidays, 'dayofyear'))
Or if you don't want to use day you could use dateshift to make a copy of d with the time components set to midnight.
isaHoliday2 = ismember(dateshift(d, 'start', 'day'), holidays)
If you need to consider only part of a day a holiday (say if you work for a company that closes at noon on the day before Thanksgiving in the US) you could use timeofday to extract the time components of d and use relational operators.
t = timeofday(d)
isAfternoon = t > duration(12, 0, 0)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!