Borrar filtros
Borrar filtros

Extreact Information from a String

1 visualización (últimos 30 días)
Christian Berwanger
Christian Berwanger el 2 de Nov. de 2017
Comentada: Jos (10584) el 5 de Ag. de 2019
Hello, I do have the following Code
d = dir('/home/user/Documents/Data*.inp');
names = {d.name}
which gives me an Array of Strings.
names(1) is like = "Data_c11_t3.322111_id01111_Units.inp"
names(2) is like = "Data_c101_t4.32111_id01112_Units.inp"
etc.
Now I want to extract the time, which is in the name after "t". How can I extract the time in such a format?
Any suggestions?

Respuesta aceptada

KL
KL el 2 de Nov. de 2017
Editada: KL el 2 de Nov. de 2017
regexp would be the best idea, I've no big experiences with it, I'll give it a shot anyway,
names = {'Data_c11_t3.322111_id01111_Units.inp';'Data_c101_t4.32111_id01112_Units.inp'};
t = regexp(names,'[0-9]+\.+[0-9*]+','match')
  4 comentarios
Guillaume
Guillaume el 3 de Nov. de 2017
To each their own opinion of course. Regular expressions are designed to extract specific patterns in strings, which, in my opinion is exactly the problem at hand.
Christian Berwanger
Christian Berwanger el 8 de Nov. de 2017
Ok. This worked for me. Thank you. I do have still a question:
Can you please explain me how this worked? I searched up regexp on Mathworks documentation. But honestly I still don't really understand why this one works.
To Guilaume: It extraced only the time. Even I have more numbers in the name. Thats why I am asking how this works as I don't see any reason to just take the numbers from time.

Iniciar sesión para comentar.

Más respuestas (2)

Jos (10584)
Jos (10584) el 3 de Nov. de 2017
Use a sscanf to read the number between the _t and _id, ignoring the varying number after the _c. Use cellfun to apply this to all cells:
names = {'Data_c11_t3.322111_id01111_Units.inp' ; 'Data_c101_t4.32111_id01112_Units.inp'} ;
T = cellfun(@(N) sscanf(N,'Data_c%*d_t%f_id'), names) % "%*d" = the * means skip
  3 comentarios
Coco Newton
Coco Newton el 2 de Ag. de 2019
@Jos, this is a great answer
How would I adapt the sscanf formatSpec to extract 130 as a numeric value from this string? I have attached the string array variable that this is example string is taken from.
'Position: (19.59862|1.8|53.84677)Rotation: (0 | 130 | 0)'
Many thanks!
Jos (10584)
Jos (10584) el 5 de Ag. de 2019
Thanks :-) Something along these lines should work for your input
str = 'Position: (19.59862|1.8|53.84677)Rotation: (0 | 130 | 0)'
A = sscanf(str, 'Position: (%*f|%*f|%*f)Rotation: (%*d | %d | %*d)')

Iniciar sesión para comentar.


KSSV
KSSV el 2 de Nov. de 2017
Editada: KSSV el 2 de Nov. de 2017
str = 'Data_c11_t3.322111_id01111_Units.inp' ;
idx = strfind(str,'_') ;
str2num(str(idx(2)+2:idx(3)-1))
Using regexp:
str = 'Data_c11_t3.322111_id01111_Units.inp' ;
N = str2num( regexprep( str, {'\D*([\d\.]+\d)[^\d]*', '[^\d\.]*'}, {'$1 ', ' '} ) ) ;
t = N(2)
  3 comentarios
KSSV
KSSV el 2 de Nov. de 2017
What is filename? Can you copy it?
Christian Berwanger
Christian Berwanger el 3 de Nov. de 2017
I do have it in a for loop. its like
for i = 1:length(names)
fileName = names(i);
idx = strfind(fileName,'_') ;
time = str2DOUBLLE(fileName(idx(2)+2:idx(3)-1));
end
so filename is in the first iteration "Data_c11_t3.322111_id01111_Units.inp"
in the second iteration: "Data_c101_t4.32111_id01112_Units.inp"
and so on.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by