change timetable variable (working day to holiday)

2 views (last 30 days)
Hi all,
i have a timetable that specifies whether a date listed is working day or not-working day, by assigning 1 for working day and 0 for not-working day.
I wish to implement the holidays to the variable 'working day' of the timetable as well by changing the working day (1) to not-working day (0) for the specified dates in the variable 'holiday'.
holiday = {.... '2016-01-01','2016-03-25','2016-03-28','2016-05-01','2016-05-05','2016-05-10','2016-10-03','2016-12-25','2016-12-26'}
How may i implement this change?
Thank you.

Accepted Answer

Star Strider
Star Strider on 26 Sep 2020
Try this:
dd = datetime(2016,01,01):days(1):datetime(2016,12,31); % Create ‘datetime’ Array
workday = ones(size(dd(:))); % Define Original ‘workday’
holiday = datetime({'2016-01-01','2016-03-25','2016-03-28','2016-05-01','2016-05-05','2016-05-10','2016-10-03','2016-12-25','2016-12-26'});
TT1 = timetable(dd(:), workday); % Create ‘timetable’ Array
TF = ~ismember(TT1.Time,holiday); % Compare ‘Time’ & ‘holiday’
TT1.workday = +TF; % The ‘+’ Converts ‘TF’ To Numeric
with:
First_5_Rows = TT1(1:5,:)
producinmg:
First_5_Rows =
5×1 timetable
Time workday
___________ _______
01-Jan-2016 0
02-Jan-2016 1
03-Jan-2016 1
04-Jan-2016 1
05-Jan-2016 1
.
  2 Comments
Sehoon Chang
Sehoon Chang on 26 Sep 2020
thanks for your advice.
Is there a possible way to assign all weekends dates (Saturdays and Sundays) along side with holiday dates the value 0? The example you have provided only includes the specified dates of holiday variables.
BR
Star Strider
Star Strider on 26 Sep 2020
My pleasure!
Sure! Add these assignments:
wkdaynr = weekday(TT1.Time); % Numeric Values For ‘weekday’
wdays = ~((wkdaynr == 1) | (wkdaynr == 7)); % Eliminate Saturday & Sunday
so the full code is now:
dd = datetime(2016,01,01):days(1):datetime(2016,12,31); % Create ‘datetime’ Array
workday = ones(size(dd(:))); % Define Original ‘workday’
TT1 = timetable(dd(:), workday); % Create ‘timetable’ Array
holiday = datetime({'2016-01-01','2016-03-25','2016-03-28','2016-05-01','2016-05-05','2016-05-10','2016-10-03','2016-12-25','2016-12-26'});
hdays = ~ismember(TT1.Time,holiday); % Compare ‘Time’ & ‘holiday’
wkdaynr = weekday(TT1.Time); % Numeric Values For ‘weekday’
wdays = ~((wkdaynr == 1) | (wkdaynr == 7)); % Eliminate Saturday & Sunday
TT1.workday = +(hdays & wdays); % Logically ‘AND’ Together, The ‘+’ Converts ‘TF’ To Numeric
with:
First_15_Rows = TT1(1:15,:)
producing:
First_15_Rows =
15×1 timetable
Time workday
___________ _______
01-Jan-2016 0
02-Jan-2016 0
03-Jan-2016 0
04-Jan-2016 1
05-Jan-2016 1
06-Jan-2016 1
07-Jan-2016 1
08-Jan-2016 1
09-Jan-2016 0
10-Jan-2016 0
11-Jan-2016 1
12-Jan-2016 1
13-Jan-2016 1
14-Jan-2016 1
15-Jan-2016 1
The first 3 lines of my code create the timetable array you already have, so just use the last 5 lines (after the blank line). Change ‘TT1’ to your timetable name.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!