My suggestion is to convert all your times to an new variable of time in matlab's datenum format. A datenum is the number of days since BCE without adjustments (or any user-defined reference). By default, the serial day 1 corresponds to 1-Jan-0000.
mydatenum = datenum(year,month,day,hour,minute,seconds);
>> datenum(now)
ans =
7.378033160737500e+05
Those numeric values can be sorted or queried for values between two dates.
The display of the datenum value is made with
datestr(mydatenum)
and user-defined format strings can be used.
>> datestr(datenum(now),'yyyymmdd HH:MM:SS.FFF')
ans =
'20200113 07:35:42.153'
The datenum command can be executed with vectors:
everyYearSince2000 = datenum(2000:2020,1,1,0,0,0);
datestr(everyYearSince2000)
ans =
21×11 char array
'01-Jan-2000'
'01-Jan-2001'
'01-Jan-2002'
'01-Jan-2003'
'01-Jan-2004'
'01-Jan-2005'
'01-Jan-2006'
'01-Jan-2007'
'01-Jan-2008'
'01-Jan-2009'
'01-Jan-2010'
'01-Jan-2011'
'01-Jan-2012'
'01-Jan-2013'
'01-Jan-2014'
'01-Jan-2015'
'01-Jan-2016'
'01-Jan-2017'
'01-Jan-2018'
'01-Jan-2019'
'01-Jan-2020'
Or for every day of the year at 11:30 am:
everyDay2000 = datenum(2020,1,1:365,11,30,0);
datestr(everyDay2000(1:20))
ans =
20×20 char array
'01-Jan-2020 11:30:00'
'02-Jan-2020 11:30:00'
'03-Jan-2020 11:30:00'
'04-Jan-2020 11:30:00'
'05-Jan-2020 11:30:00'
'06-Jan-2020 11:30:00'
'07-Jan-2020 11:30:00'
'08-Jan-2020 11:30:00'
'09-Jan-2020 11:30:00'
'10-Jan-2020 11:30:00'
'11-Jan-2020 11:30:00'
'12-Jan-2020 11:30:00'
'13-Jan-2020 11:30:00'
'14-Jan-2020 11:30:00'
'15-Jan-2020 11:30:00'
'16-Jan-2020 11:30:00'
'17-Jan-2020 11:30:00'
'18-Jan-2020 11:30:00'
'19-Jan-2020 11:30:00'
'20-Jan-2020 11:30:00'
3 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/500075-how-to-filter-data-by-date#comment_785591
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/500075-how-to-filter-data-by-date#comment_785591
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/500075-how-to-filter-data-by-date#comment_785592
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/500075-how-to-filter-data-by-date#comment_785592
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/500075-how-to-filter-data-by-date#comment_785602
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/500075-how-to-filter-data-by-date#comment_785602
Sign in to comment.