define time period for each year

1 回表示 (過去 30 日間)
Sehoon Chang
Sehoon Chang 2020 年 11 月 15 日
コメント済み: Walter Roberson 2020 年 11 月 19 日
Hi all,
I wish to re-define my time period.
The initial time period is as following:
The time period ranges from 01-10 (1st of Oct.) till 30-04 (30th of Apr.).
The range is defined by months only. The array 'Date' contains datetime value that is in yyyy-MM-dd format.
% starting month
sm = 5;
% ending month
em = 9;
% starting day
fd = 1;
% ending day
ld = 30;
PERIOD_1 = find(month(Date)<sm) | month(Date)>em)
PERIOD_2 = find(month(Date)>(sm-1) & month(Date)<(em+1))
As for a new time period, I wish to use both day values ('starting day' and 'ending day') as well, because if not...
i won't be able to define period that does not start on the 1st of a month (e.g. 04-10 till 27-04).
Thank you!

採用された回答

Walter Roberson
Walter Roberson 2020 年 11 月 15 日
[y, m, d] = ymd(YourDataTable.Date);
mask = m >= sm & m <= em & (m > sm | d >= fd) & (m < em | d <= ld);
selected_entries = YourDataTable(mask,:);
  2 件のコメント
Peter Perkins
Peter Perkins 2020 年 11 月 19 日
I was gonna say that it should also be possible to simplify this logical test by using 'dayofyear'. That makes it just
d = day(YourDataTable.Date,'dayofyear');
mask = d < 120 | d >= 274
but then ... stupid leap years! Still, you could modify that a bit to work.
Walter Roberson
Walter Roberson 2020 年 11 月 19 日
Though the poster did say that the range was defined by month only, so
m = month(YourDataTable.Date);
mask = m <= 4 | m >= 10;
selected_entries = YourDataTable(mask,:);
I think my earlier code was selecting in the opposite sense, as I did not notice that they wanted October to April.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by