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?

채택된 답변

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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by