How can i extract the numbers from string?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Happy PhD
el 7 de Nov. de 2022
Im trying to extract the numbers from a cell array? How can i do this?
cell array:
filename =
1×12 cell array
Columns 1 through 4
{'104p8.png'} {'105.png'} {'105p2.png'} {'105p4.png'}
Columns 5 through 8
{'105p6.png'} {'105p8.png'} {'106.png'} {'106p2.png'}
Columns 9 through 12
{'106p4.png'} {'106p6.png'} {'106p8.png'} {'107.png'}
Thanks
4 comentarios
Respuesta aceptada
Stephen23
el 7 de Nov. de 2022
Editada: Stephen23
el 7 de Nov. de 2022
Here are two approaches:
C = {'104p8.png','105.png','105p2.png','105p4.png','105p6.png','105p8.png','106.png','106p2.png','106p4.png','106p6.png','106p8.png','107.png'}
V = str2double(strrep(strrep(C,'.png',''),'p','.'))
V = str2double(regexprep(C,{'\.png$','p'},{'','.'},'ignorecase')) % more robust
0 comentarios
Más respuestas (1)
Marcel
el 7 de Nov. de 2022
Editada: Marcel
el 7 de Nov. de 2022
Hi i made a test script and came up with the following code. I found a solution here
example = cellstr(["1024.png", "image1003.png", "photo-1234.png"])
for i=1:length(example)
name = example{i};
numbers = str2double(extract(name, digitsPattern))
end
example =
1×3 cell array
{'1024.png'} {'image1003.png'} {'photo-1234.png'}
numbers =
1024
numbers =
1003
numbers =
1234
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!