Clear Filters
Clear Filters

filtering a table with dates

4 views (last 30 days)
AA
AA on 25 Sep 2015
Commented: Walter Roberson on 27 Sep 2015
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

Answers (1)

Walter Roberson
Walter Roberson on 25 Sep 2015
Edited: Walter Roberson on 27 Sep 2015
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 Comments
AA
AA on 27 Sep 2015
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 on 27 Sep 2015
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,:);

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!