MATLAB Answers

How to filter data by date?

58 views (last 30 days)
Angelavtc
Angelavtc on 13 Jan 2020
Commented: Angelavtc on 13 Jan 2020
I have a table where the first column contains the date and in the following ones are different variables related with this date. My question is how can I extract information from the table for every season? This means, to create several tables that filters only the observations related with spring 2010 (March to May), summer 2010 (June to August), autumn 2010 ( September to November) and winter 2010 (December 2010 to February 2011)?
I am new using matlab and what I did first is to convert the table to a timetable, but then I dont know how to make the date filter. My date is shown as day/month/year, for example, 26/01/2010.
Thanks in advance!
My data looks likes this:
Captura de pantalla 2020-01-13 a la(s) 1.32.38 p. m..png

  3 Comments

Ajay Kumar
Ajay Kumar on 13 Jan 2020
Could you upload the data file?
Bhaskar R
Bhaskar R on 13 Jan 2020
can share some small portion of data in mat file?
Bhaskar R
Bhaskar R on 13 Jan 2020
Try my answer, if not worked out share mat file

Sign in to comment.

Accepted Answer

Bhaskar R
Bhaskar R on 13 Jan 2020
Edited: Bhaskar R on 13 Jan 2020
Suppose your timetable data in the variable td
spring = td(timerange('2010-04-01', '2010-05-31'), :); % spring time data
summer = td(timerange('2010-06-01', '2010-08-31'), :); % summer time data
autumn = td(timerange('2010-09-01', '2010-10-31'), :); % autumn time data
winter = td(timerange('2010-12-01', '2011-02-28'), :); % winter time data

  1 Comment

Angelavtc
Angelavtc on 13 Jan 2020
This works perfect! Thank you :)

Sign in to comment.

More Answers (1)

Meg Noah
Meg Noah on 13 Jan 2020
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'

  0 Comments

Sign in to comment.