How can you split a table (or array) based on similar values in a column?
Show older comments
Hi,
I have a data set with longitudinal and latitudinal data per time stamp (matlab time) and an id number per gps device. (lon, lat, timestamp, id) I would like to do two things:
- split this file into separate files by id and order them based on the corresponding time stamp.
- sort the complete file first on id, then on timestamp
Probably it is very easy, but I am quite new to Matlab... Thank you very much!
Answers (1)
dpb
on 29 Sep 2016
- is most simply accomplished by first doing (you guessed it...)
- sort by id
Once you've done that, then simply find the position at which the ID changes and write the subsections to a file.
For the first part, see
doc sortrows % note carefully the second optional argument, COL, it'll be your lifesaver
Second part, there are a myriad of ways you can start with the simplest which is simply to take the difference of the column containing the ID and see where it's nonzero. Those locations will be where there's a change in ID, hence the breakpoint for the various files. Note you will want to augment the difference vector result with a placeholder 0 as there's one less difference in a series than the number of elements in the series. That is
ix=find([0 diff(ID)]);
will be the locations for the change.
There's a tutorial on writing files and generating names at the FAQ <How_can_I_process_a_sequence_of_files?>
The biggest thing is to just jump in and start...and, of course, be sure to have a backup for the initial data files so you're sure to be able to recover from something silly like naming an output file the same as the input or the like....and, yes, everybody here will have done something of the sort somewhere along their progression. :)
Categories
Find more on Shifting and Sorting Matrices 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!