change timetable variable (working day to holiday)
1 回表示 (過去 30 日間)
古いコメントを表示
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/366859/image.jpeg)
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.
0 件のコメント
採用された回答
Star Strider
2020 年 9 月 26 日
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 件のコメント
Star Strider
2020 年 9 月 26 日
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.
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!