I can't get millisecond accuracy from string based timestamp - not sure what I'm doing wrong
3 views (last 30 days)
Show older comments
I have a load of data that's timestamped that I'm looking to manipulate using mainly durations. Issue is that the data is in 3 digit millisecond precisions and nothing less precise will do and everytime I try to convert my string cells to timestamps, it round everything to the nearest second and adds the current date to the front. I don't actually need the date at all.
This is my raw input, its a cell array of timestamps.
My code:
> infmt = 'HH:mm:ss.SSS';
> formatted_timestamps = datetime(raw_timestamps, 'InputFormat', infmt);
My output:

I have tried appending the values with less millisecond digits with zeros to ensure they're all the same length as strings, but all that did was remove the NaT values in output. I have spent hours trying to figure it out but all the forums and support docs say that what I have done is correct and as no on e else appears to have this issue I'm assuming its perhaps something more fundamental that I'm doing wrong?
Thanks in advance!
0 Comments
Accepted Answer
Ameer Hamza
on 20 Nov 2020
Also set the format property to display them in the format you want
formatted_timestamps = datetime(raw_timestamps, 'InputFormat', infmt, 'Format', infmt);
2 Comments
More Answers (1)
Steven Lord
on 20 Nov 2020
If you don't have date information associated with your timestamps, I would use duration instead of datetime.
S = ["11:00:00.147"; "11:00:00.668"; "11:00:00.932"; "11:00:04"]
fmt = 'hh:mm:ss.SSS';
d = duration(S, 'InputFormat', fmt)
d2 = duration(S, 'InputFormat', fmt, 'Format', fmt)
0 Comments
See Also
Categories
Find more on Downloads 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!