filtering out two months with a code

2 ビュー (過去 30 日間)
AA
AA 2017 年 11 月 1 日
回答済み: dpb 2017 年 11 月 1 日
dateless = Dates(nomatch);
datelike = Dates(~nomatch);
dateliketime=datetime(datelike);
liatime=dateliketime.Month == 10;
datelike1= datelike(liatime,1);
Here I select the month November from the group that is available. What if I want to select two months, i.e. November and October? Is there any way to display this?

採用された回答

dpb
dpb 2017 年 11 月 1 日
Month1 == 10; % make variables instead of hardcoded
Month2 == 11; % "magic" numbers
datelike1= datelike(iswithin(dateliketime.Month,Month1,Month2));
where iswithin is my "syntactic sugar" utility
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
If you convert your dates to datetime class, there is a "veritable plethora" of functions specifically for handling dates, date arithmetic and logic, including isbetween

その他の回答 (1 件)

Cam Salzberger
Cam Salzberger 2017 年 11 月 1 日
In this code snippet, liatime will be a logical array, with true anywhere dateliketime.Month is 10, and false anywhere it isn't. So a good place to start looking would probably be under MATLAB's logical operations.
For your specific question, the logical OR operator (non-short-circuit version) should be sufficient:
liatime = dateliketime.Month == 10 | dateliketime.Month == 11;
-Cam

カテゴリ

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