filtering a table with dates

4 次查看(过去 30 天)
AA
AA 2015-9-25
I have a table with 3 columns. COLUMN one has a serial date number. Column two has the time and column three has a value. I want to filter the table so that I got only serial date numbers in the range of 23. September 2015, 3 pm to 24. September 2015, 5 pm, 3. june 2014, 4 pm to 5. June 2014, 7 pm.
thanks for your help

回答(1 个)

Walter Roberson
Walter Roberson 2015-9-25
编辑:Walter Roberson 2015-9-27
datelimstr = {'23. September 2015, 3 pm', '24. September 2015, 5 pm', '3. june 2014, 4 pm', '5. June 2014, 7 pm'};
datelim = datenum(datelimstr, 'DD mmmm YYYY, HH PM');
SD = YourTable.SerialDate;
inrange = (SD >= datelim(1) & SD <= datelim(2)) | (SD >= datelim(3) & SD <= datelim(4));
SubTable = YourTable(inrange,:);
This presumes that the "serial date number" includes the time in it and not just the integer part. If not then we need to know what the format is for the time column.
  2 个评论
AA
AA 2015-9-27
it doesnt work, i get a table with zeros. can you redefine the code without the time column. i deleted the time column. i only need the date. thanks
Walter Roberson
Walter Roberson 2015-9-27
I had a typo, but otherwise it should have worked, provided the date strings were in the format you posted, complete with commas and period. Anyhow, without the time information:
datelimstr = {'23. September 2015', '24. September 2015', '3. june 2014', '5. June 2014'};
datelim = datenum(datelimstr, 'DD. mmmm YYYY');
SD = YourTable.SerialDate;
inrange = (SD >= datelim(1) & SD <= datelim(2)) | (SD >= datelim(3) & SD <= datelim(4));
SubTable = YourTable(inrange,:);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by