how to filter dates from already present column?

5 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by