Borrar filtros
Borrar filtros

Finding characters in a string / filename

5 visualizaciones (últimos 30 días)
L'O.G.
L'O.G. el 3 de Abr. de 2022
Comentada: L'O.G. el 3 de Abr. de 2022
How would I return as a double the number after the letter in the following file name? For instance, in the following, I would like to return 44 only.
'A123_T44_1.txt'

Respuesta aceptada

Image Analyst
Image Analyst el 3 de Abr. de 2022
If your filename naming protocol is fixed, so that the second number is always in position 7 and 8, you can use str2double():
fileName = 'A123_T44_1.txt';
number2 = str2double(fileName(7:8))
number2 = 44
  3 comentarios
Image Analyst
Image Analyst el 3 de Abr. de 2022
So the format is letter, then any number of numbers, then '_T', then any number of numbers, then an underline, and finally some number of numbers? And you want the number between the first and second underline?
fileName = 'A123_T44_1.txt';
underlineLocations = strfind(fileName, '_');
number2 = str2double(fileName(underlineLocations(1) + 2 : underlineLocations(2) - 1))
number2 = 44
It's just an alternative in case you find the scanf/regexp stuff too cryptic and want something more straightforward.
L'O.G.
L'O.G. el 3 de Abr. de 2022
Thank you so much for explaining all that.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by