create a variable based on timetable's date period
Show older comments
Hello dear advisors.
i wish to create a new variable (HP) for my timetable (TT) which datetime covers for multiple years.
For the periods in between 01.October till 30.April of each year available within the timetable, each timestamp shall get the value of 1 as a variable HP.
And as for the rest (01.May till 30.September), each remaining timestamps shall get the value of 0 as a variable of HP.
for example:

thanks!
Accepted Answer
More Answers (1)
Steven Lord
on 1 Oct 2020
Edited: Steven Lord
on 1 Oct 2020
Using the sample timetable Star Strider created:
DT = (datetime([2017 01 01]):caldays(1):datetime([2020 01 01])).';
HP = ones(size(DT));
TT = timetable(DT,HP);
Get the list of months for each row in the timetable:
M = month(TT.DT);
Determine which months are between May (5th month) and September (9th month) inclusive.
isInRangeFor0 = ismember(M, 5:9);
Create the new variable. Since isInRangeFor0 is true when we want the new variable to have value 0 and vice versa, we can just negate that variable.
TT.newvar = ~isInRangeFor0;
1 Comment
Star Strider
on 1 Oct 2020
That’s definitely an interesting alternative.
Using ismember for the months didn’t occur to me. I’ll definitely keep that in mind!
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!