Could not recognize the format of the date/time text

5 ビュー (過去 30 日間)
NUR DALILA
NUR DALILA 2022 年 10 月 4 日
コメント済み: NUR DALILA 2022 年 10 月 5 日
I have a timetable with a time column containing microsecond resolution. The time column contains something like this:
"07/04/2021 07:55:27.502.118"
"07/04/2021 07:55:27.502.196"
"07/04/2021 07:55:27.502.274"
"07/04/2021 07:55:27.502.352"
"07/04/2021 07:55:27.502.430"
"07/04/2021 07:55:27.502.508"
How can i process the data using datetime function?

採用された回答

Stephen23
Stephen23 2022 年 10 月 4 日
C = ["07/04/2021 07:55:27.502.118"
"07/04/2021 07:55:27.502.196"
"07/04/2021 07:55:27.502.274"
"07/04/2021 07:55:27.502.352"
"07/04/2021 07:55:27.502.430"
"07/04/2021 07:55:27.502.508"];
D = datetime(regexprep(C,'\.(\d+)$','$1'), 'InputFormat','d/M/y H:m:s.SSSSSS');
Checking:
D.Format = 'MM/dd/yyyy HH:mm:ss.SSSSSS'
D = 6×1 datetime array
04/07/2021 07:55:27.502117 04/07/2021 07:55:27.502196 04/07/2021 07:55:27.502274 04/07/2021 07:55:27.502352 04/07/2021 07:55:27.502430 04/07/2021 07:55:27.502507
  1 件のコメント
NUR DALILA
NUR DALILA 2022 年 10 月 5 日
thank you for your kind help :)

サインインしてコメントする。

その他の回答 (1 件)

Eric Delgado
Eric Delgado 2022 年 10 月 4 日
You can't. The precision of datetime is milliseconds, but you can use regexp.
inData = "07/04/2021 07:55:27.502.118";
regData = regexp(inData, '(?<timestamp_ms>\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}.\d{3}).(?<us>\d*)', 'names')
regData = struct with fields:
timestamp_ms: "07/04/2021 07:55:27.502" us: "118"
outData = datetime(regData.timestamp_ms, 'InputFormat', 'dd/MM/yyyy HH:mm:ss.SSSSSS') + seconds(str2double(regData.us)/1e+6)
outData = datetime
07-Apr-2021 07:55:27
format long
second(outData)
ans =
27.502117999999999
  2 件のコメント
Eric Sofen
Eric Sofen 2022 年 10 月 4 日
To be clear, datetime supports more than milliseconds. In fact, datetime guarantees at least nanosecond precision.
NUR DALILA
NUR DALILA 2022 年 10 月 5 日
thank you for your kind help :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by