how to filter dates from already present column?

4 ビュー (過去 30 日間)
Suresh R
Suresh R 2021 年 10 月 27 日
回答済み: Arjun 2024 年 10 月 11 日
how to filter dates from already present column(below program) with starting date as 19920102 and ending date as 20110930
Qtfdates = {};
for year =1980:2020
for month=3:3:12
Qtfdates{end+1}= datestr(nweekdate(3, 6, year, month));
end
end
vertcat(Qtfdates{:})

回答 (1 件)

Arjun
Arjun 2024 年 10 月 11 日
I understand that you have a column vector containing dates and you want to filter dates based on a specified date range.
To filter dates from existing cell array of date strings (Qtfdates) based on specified date range, you can follow these steps:
  • Conver the date strings to MATLAB’s serial date numbers for ease in comparison.
  • Define starting and ending dates as serial date numbers.
  • Use logical indexing to filter the dates within the specified range.
Kindly refer to the following code for better understanding:
Qtfdates = {};
for year = 1980:2020
for month = 3:3:12
Qtfdates{end+1} = datestr(nweekdate(3, 6, year, month));
end
end
% Convert Qtfdates to a column vector
dateStrings = vertcat(Qtfdates{:});
% Convert date strings to serial date numbers
dateNumbers = datenum(dateStrings);
startDate = datenum('19920102', 'yyyymmdd');
endDate = datenum('20110930', 'yyyymmdd');
% Filter dates within the specified range
filteredDates = dateStrings(dateNumbers >= startDate & dateNumbers <= endDate, :);
disp(filteredDates);
Kindly explore the documentation on “datenum” function for conversion to serial date numbers: https://www.mathworks.com/help/releases/R2021b/matlab/ref/datenum.html
I hope this will help!

カテゴリ

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