How to parse an Nx1 string array without looping through N
Mostrar comentarios más antiguos
I have an Nx1 string array, and I can't figure out how to extract 6 chunks of text out of it and into an Nx6 cell array. The text elements are numbers, but it's simplest to not treat them as numbers at this juncture.
Here is a toy version of the string array, together with code that correctly parses out the necessary elements of CCYYMMDD and hhmm from the first element of the string array:
stringFile = ["nsasondewnpnC1.b1.20020428.184800.cdf"; ...
"nsasondewnpnC1.b1.20020428.220500.cdf"; ...
"nsasondewnpnC1.b1.20020428.235900.cdf"; ...
"nsasondewnpnC1.b1.20020429.013100.cdf"; ...
"nsasondewnpnC1.b1.20020429.182500.cdf"];
charLaunch = textscan(stringFile(1),'%*18c %2c %2c %2c %2c %*c %2c %2c');
charLaunch =
1×6 cell array
{'20'} {'02'} {'04'} {'28'} {'18'} {'48'}
However, both
charLaunchAll = textscan(stringFile,'%*18c %2c %2c %2c %2c %*c %2c %2c');
and
charLaunchAll = cell(5,6);
charLaunchAll = textscan(stringFile(:),'%*18c %2c %2c %2c %2c %*c %2c %2c');
generate the same error message:
Error using textscan
First input must be a valid file-id or non-empty character vector.
Is there a way to extract these pieces of texts out of every array member without building a loop?
Respuesta aceptada
Más respuestas (1)
Mohammad Sami
el 23 de Abr. de 2020
Since the pattern in your string seems to be the same, you can use the format specification to convert the string directly to datetime as follows.
stringFile = ["nsasondewnpnC1.b1.20020428.184800.cdf"; ...
"nsasondewnpnC1.b1.20020428.220500.cdf"; ...
"nsasondewnpnC1.b1.20020428.235900.cdf"; ...
"nsasondewnpnC1.b1.20020429.013100.cdf"; ...
"nsasondewnpnC1.b1.20020429.182500.cdf"];
fmt = "'nsasondewnpnC1.b1.'yyyyMMdd'.'HHmmss'.cdf'";
% the constant portion of your string is enclosed in 'single quotes';
d = datetime(stringFile,'InputFormat',fmt);
1 comentario
Leslie
el 23 de Abr. de 2020
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!