How to remove rows in a table
1 view (last 30 days)
Show older comments
Hi, I have a table in Matlab with many rows. There is one column called datetime. Same records can appear in the table multiple time with only very different times, but same date. I would like to remove all the Same records but keep the one maximum datetime within the same date.
How to do that?
Thanks,
Jennifer
0 Comments
Answers (1)
dpb
on 29 Feb 2016
Edited: dpb
on 1 Mar 2016
Ensure the data are sorted in ascending date/time then use unique. The default for the optional named parameter 'occurrence' is 'last' so you don't even have to specify it; you'll get the desired result automagically.
ADDENDUM
OK, with the additional discussion and particularly the reminder that behavior has changed since my release here of R2012b, if the dates are in the one column then you do need to separate out into date without the time...assuming it's t, it looks like simplest will still be to revert to date numbers as there's not a single property of datetime that is the numeric equivalent...or you can use the 'rows' option -- I guess that's not much different--
[~,ia]=unique([t.year t.month t.day],'rows','last');
will be the row index into the table of the desired last time (again, assuming the input is in ascending time order).
Hence, your desired result will then simply be
t=t(ia); % save the desired data in place
or, of course, save to a new dataset variable if do need the old as well at the same time.
3 Comments
Image Analyst
on 29 Feb 2016
Make it easy for us to help you and give us the table so we can try something. You might have to parse the date-time string into separate columns for date and time.
dpb
on 29 Feb 2016
Edited: dpb
on 1 Mar 2016
No problem; simply do the selection over the date column and keep the index vector returned.
ADDENDUM As IA points out (and I assumed originally) you do need the date and times separated so unique does its thing on it alone; else't every time will be unique so no discrimination will occur. Also after (I think?) R2012b, the behavior of unique was modified to return 'first' rather than 'last' as the default for repeated occurrences so you shouldn't rely on default behavior. 'Tis a pain when TMW does this to us...and scary if one has existing code relying on what was then previously documented behavior. BTW, TMW, the doc for current release is extremely difficult to interpret in this area; examples from R2012b on repeated instances have been removed and nothing explicit really replaced them... :( If you didn't know the earlier and had read it I'm not sure one could figure out what the behavior is now simply from the doc's as currently written other than by trial and error...
See Also
Categories
Find more on Tables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!