datetime InputFormat working for one string-to-datetime conversion, failing for very similar example

1 回表示 (過去 30 日間)
Given a 12×1 string array of file names
testNamestr = ...
["nsasondewnpnC1.b1.20210124.230100.cdf";
"nsasondewnpnC1.b1.20210125.053000.cdf";
"nsasondewnpnC1.b1.20210131.230100.cdf";
"nsasondewnpnC1.b1.20210201.053000.cdf";
"nsasondewnpnC1.b1.20210204.173000.cdf";
"nsasondewnpnC1.b1.20210204.230100.cdf";
"nsasondewnpnC1.b1.20210205.053000.cdf";
"nsasondewnpnS01.b1.20210123.214300.cdf";
"nsasondewnpnS01.b1.20210126.222700.cdf";
"nsasondewnpnS01.b1.20210127.220900.cdf";
"nsasondewnpnS01.b1.20210203.213700.cdf";
"nsasondewnpnS01.b1.20210204.211800.cdf"];
I need to extract the date & time portion of the string (12345678.123456) from the rest and convert it to datetime. In the strings containing "C1", there are 18 characters before the date & time and the entire string is 37 characters long. In the strings containing "S01", there are 19 characters before the date & time and the entire string is 38 characters long.
I have done this:
index18 = find(strlength(testNamestr)==37); % indices of the 'C1' file names
index19 = find(strlength(testNamestr)==38); % indices of the 'S01' file names
namesprefix18 = testNamestr{index18(1)}(1:18); % text before date&time info, 'C1' files
namesprefix19 = testNamestr{index19(1)}(1:19); % text before date&time info, 'S01' files
% Yields
% namesprefix18 =
% 'nsasondewnpnC1.b1.'
% namesprefix19 =
% 'nsasondewnpnS01.b1.'
% Build input formats for filenames, 'C1' and 'S01' files
armfmt18 = ...
strcat("'",namesprefix18,"'",'yyyyMMdd','.','HHmmss',"'",'.cdf',"'");
armfmt19 = ...
strcat("'",namesprefix19,"'",'yyyyMMdd','.','HHmmss',"'",'.cdf',"'");
% Initialize variable to hold datetime values,
% then fill it by converting strings extracted from testNamestr
testLaunchDatetime = NaT(12,1);
testLaunchDatetime(index18) = ...
datetime(testNamestr(index18),'InputFormat',armfmt18)
% Yields
% testLaunchDatetime =
% 12×1 datetime array
% 24-Jan-2021 23:01:00
% 25-Jan-2021 05:30:00
% 31-Jan-2021 23:01:00
% 01-Feb-2021 05:30:00
% 04-Feb-2021 17:30:00
% 04-Feb-2021 23:01:00
% 05-Feb-2021 05:30:00
% NaT
% NaT
% NaT
% NaT
% NaT
testLaunchDatetime(index19) = ...
datetime(testNamestr(index19),'InputFormat',armfmt19)
% Yields
% Error using datetime (line 636)
% Unable to convert the text to datetime using the format
% ''nsasondewnpnS01.b1.'yyyyMMdd.HHmmss'.cdf''. If the date/time text
% contain day, month, or time zone names in a language foreign to the
% 'en_US' locale, those might not be recognized. You can specify a
% different locale using the 'Locale' parameter.
Why is this failing for one case but not the other?

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 13 日
編集済み: Walter Roberson 2021 年 3 月 13 日
The error is in processing the quoted 'S' character. 'S' is the only alphabetic character that the processing fails for.
It is probably a bug.
Since you already have the indices that tell you how long the name is, trim out the useful part using extractBetween(String, first, last)
C=['A':'Z','a':'z'];
for K=C; try; datetime(K+".2", 'InputFormat', "'"+K+"'.2"); fprintf('okay %c\n', K); catch ME; fprintf('not %c\n', K); end; end
okay A okay B okay C okay D okay E okay F okay G okay H okay I okay J okay K okay L okay M okay N okay O okay P okay Q okay R not S okay T okay U okay V okay W okay X okay Y okay Z okay a okay b okay c okay d okay e okay f okay g okay h okay i okay j okay k okay l okay m okay n okay o okay p okay q okay r okay s okay t okay u okay v okay w okay x okay y okay z
  5 件のコメント
Steven Lord
Steven Lord 2021 年 3 月 13 日
For future reference, if you find what you believe is a bug you can report it using the Contact Support link on the Support section of this website. Posting here means that MathWorks staff are likely to see it but submitting the report through Support makes it sure (unless there's a bug in our system) that at least the Support staff will see it. They can enter it into the bug database (or explain why it's not a bug) and potentially suggest a workaround.

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

その他の回答 (1 件)

the cyclist
the cyclist 2021 年 3 月 13 日
編集済み: the cyclist 2021 年 3 月 13 日
FYI, you could also have done this using regular expression search:
testNamestr = ...
["nsasondewnpnC1.b1.20210124.230100.cdf";
"nsasondewnpnC1.b1.20210125.053000.cdf";
"nsasondewnpnC1.b1.20210131.230100.cdf";
"nsasondewnpnC1.b1.20210201.053000.cdf";
"nsasondewnpnC1.b1.20210204.173000.cdf";
"nsasondewnpnC1.b1.20210204.230100.cdf";
"nsasondewnpnC1.b1.20210205.053000.cdf";
"nsasondewnpnS01.b1.20210123.214300.cdf";
"nsasondewnpnS01.b1.20210126.222700.cdf";
"nsasondewnpnS01.b1.20210127.220900.cdf";
"nsasondewnpnS01.b1.20210203.213700.cdf";
"nsasondewnpnS01.b1.20210204.211800.cdf"];
datetimeCellArray = regexp(testNamestr,'(?<=b1.)\d*.\d*','match');
testLaunchDatetime = datetime(string(datetimeCellArray),'InputFormat',['yyyyMMdd','.','HHmmss'])
testLaunchDatetime = 12×1 datetime array
24-Jan-2021 23:01:00 25-Jan-2021 05:30:00 31-Jan-2021 23:01:00 01-Feb-2021 05:30:00 04-Feb-2021 17:30:00 04-Feb-2021 23:01:00 05-Feb-2021 05:30:00 23-Jan-2021 21:43:00 26-Jan-2021 22:27:00 27-Jan-2021 22:09:00 03-Feb-2021 21:37:00 04-Feb-2021 21:18:00
The regular expression in this case uses a "look-ahead", finding the digits you need (with the decimal point in there) that immediately follow the "b1."
But this is arguably less elegant than the extractBetween solution:
datetimeStrings = extractBetween(testNamestr,"b1.",".cdf");
testLaunchDatetime = datetime(datetimeStrings,'InputFormat',['yyyyMMdd','.','HHmmss'])
testLaunchDatetime = 12×1 datetime array
24-Jan-2021 23:01:00 25-Jan-2021 05:30:00 31-Jan-2021 23:01:00 01-Feb-2021 05:30:00 04-Feb-2021 17:30:00 04-Feb-2021 23:01:00 05-Feb-2021 05:30:00 23-Jan-2021 21:43:00 26-Jan-2021 22:27:00 27-Jan-2021 22:09:00 03-Feb-2021 21:37:00 04-Feb-2021 21:18:00
  1 件のコメント
Leslie
Leslie 2021 年 3 月 13 日
Thanks! Happy to learn other options. The one I was using was the most elegant reply I got 11 months ago when I was first trying to figure out how to do this (before I knew about the existence of these "supplemental" files).

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by