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

2 次查看(过去 30 天)
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!

采纳的回答

Ameer Hamza
Ameer Hamza 2020-11-20
Also set the format property to display them in the format you want
formatted_timestamps = datetime(raw_timestamps, 'InputFormat', infmt, 'Format', infmt);

更多回答(1 个)

Steven Lord
Steven Lord 2020-11-20
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

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by