MATLAB Answers

Convert timestamps in CSV file to seconds using readtable and table2timetable

50 views (last 30 days)
YH on 11 Mar 2018
Commented: YH on 13 Mar 2018
I found several solutions for converting timestamps to seconds (by eliminating year, month, day...), but none of them worked. How can I use readtable() and table2timetable() for converting the timestamps in my CSV file to seconds? Attached you can find the CSV file. My goal is to plot gX, gY, gZ in terms of time. What is the most efficient way of applying that?


dpb on 11 Mar 2018
First you need to create the input file with sufficient precision to differentiate between samples (or eliminate the repeated timestamps in some fashion).
After you do that, I suspect you'll have better luck using the timeseries object than the datetime or timetable to use linear time instead of calendar time for plotting as the duration time object is hamstrung for less than seconds intervals it appears.
Or, just revert to the old datenum and treat the absicssa as purely numeric and fixup the axes using the datetimeruler manually.
YH on 11 Mar 2018
thanks for the reply. Which function should I use to read the CSV file and import the TimeStamps data to timeseries? Can you give example for using timeseries as required?

Sign in to comment.

Accepted Answer

Jeremy Hughes
Jeremy Hughes on 11 Mar 2018
This will work as long as the times are on the same day (as in the file).
T = readtable(file,'Format','%{dd-MMM-uuuu HH:mm:ss.SS}D%f%f%f')
times = timeofday(T.TimeStamp);
times.Format = 'hh:mm:ss.SSS';
TT = table2timetable(T(:,2:end),'RowTimes',times)
you can also subtract datetimes, which would make them elapsed times since start. Not sure what you're looking for.
times = T.TimeStamp - T.TimeStamp(1)


Show 7 older comments
YH on 12 Mar 2018
thanks Eric for the clarification. Now the time axis looks much better.
dpb, the reason for having redundant values is because I'm using the sensor of my phone (accelerometer) with very high resolution (having 11808 data entries for 200 steps..). And I guess having background applications running at the same time make my phone gets slower and unable of continuing measuring in such high resolution for data acquisition. The precision problem can be solved, as you said, by eliminating the redundant values using
unique(T, 'stable');
dpb on 13 Mar 2018
Depending upon what you're trying to do with the data you might also be able to interpolate the time vector to backfill the missing times accurately enough...

Sign in to comment.

More Answers (0)

Sign in to answer this question.