Datetime missing milliseconds values showing up as Nat
古いコメントを表示
Hello,
I have a table with a column of data with the format Datetime.
-ds.SelectedFormats(1)={'%{yyyy-MM-dd HH:mm:ss.SS}D'};
Some values are showing up as NaT because they are missing values in the milliseconds(.SS) place. i.e '2018-03-15 08:49:28.(blank)' is showing up as NaT.
Could I add a statement to read through the table & Input .00 for the milliseconds places that are blank? or make an if statement to change the format to yyyy-MM-dd HH:mm:ss?
Thanks for any suggestions.
回答 (1 件)
Peter Perkins
2018 年 3 月 28 日
Anthony, it's not clear what your starting point is. If you have a file, some of whose rows contain timestamps with ms, some without, I think you can read in that field as text, and then convert to datetimes with two passes. Something like
dt = datetime(timestamps,'Format',fmt1);
i = isnat(dt);
dt(i) = datetime(timestamps(i),'Format',fmt2);
It's also possible that you could add .000 to the text timestamps, and it might even be easy by looking at the length of each element.
7 件のコメント
Walter Roberson
2018 年 3 月 28 日
If you have the entries in text format then
timestamps_as_text = regexrep(timestamps_as_text, '(:\d\d)$', '$1.00');
and then it should work with a single format.
Anthony Kenaga
2018 年 3 月 28 日
編集済み: Walter Roberson
2018 年 3 月 28 日
Anthony Kenaga
2018 年 3 月 28 日
編集済み: Walter Roberson
2018 年 3 月 28 日
Walter Roberson
2018 年 3 月 28 日
? The two formats you are using appear to be the same? I would have expected the second format to not have the .SS ?
Anthony Kenaga
2018 年 3 月 29 日
Walter Roberson
2018 年 3 月 29 日
Historically, datenum() without any format string has been known to be adaptable for any one consistent format, but having difficulty with mixed formats of input. Perhaps something similar is going on here with datetime(), that it is adapting as long as the input is consistent since you are only passing in a consistent subset of the data for the second call ?
Anthony Kenaga
2018 年 4 月 3 日
カテゴリ
ヘルプ センター および File Exchange で Time Series Events についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!