how to filter dates from already present column?

5 visualizaciones (últimos 30 días)
Suresh R
Suresh R el 27 de Oct. de 2021
Respondida: Arjun el 11 de Oct. de 2024
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{:})

Respuestas (1)

Arjun
Arjun el 11 de Oct. de 2024
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!

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by