datetime InputFormat working for one string-to-datetime conversion, failing for very similar example
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
the cyclist
le 13 Mar 2021
Modifié(e) : the cyclist
le 13 Mar 2021
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'])
1 commentaire
Leslie
le 13 Mar 2021
Catégories
En savoir plus sur Cell Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!