필터 지우기
필터 지우기

Finding characters in a string / filename

조회 수: 5 (최근 30일)
L'O.G.
L'O.G. 2022년 4월 3일
댓글: L'O.G. 2022년 4월 3일
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'

채택된 답변

Image Analyst
Image Analyst 2022년 4월 3일
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
Image Analyst
Image Analyst 2022년 4월 3일
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. 2022년 4월 3일
Thank you so much for explaining all that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by