I can't get millisecond accuracy from string based timestamp - not sure what I'm doing wrong

3 views (last 30 days)
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!

Accepted Answer

Ameer Hamza
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);

More Answers (1)

Steven Lord
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"]
S = 4×1 string array
"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)
d = 4×1 duration array
11:00:00 11:00:00 11:00:00 11:00:04
d2 = duration(S, 'InputFormat', fmt, 'Format', fmt)
d2 = 4×1 duration array
11:00:00.147 11:00:00.668 11:00:00.932 11:00:04.000

Categories

Find more on Downloads in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!