datetime InputFormat working for one string-to-datetime conversion, failing for very similar example
2 ビュー (過去 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?
0 件のコメント
採用された回答
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
5 件のコメント
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
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'])
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'])
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!