Extracting number value from string from string using sscanf()

조회 수: 5 (최근 30일)
Dimitris M
Dimitris M 2012년 8월 26일
Hello
I have a problem trying to extract a number from a string looking like this
Seg_1169_Edgefun_1_5_2_15_90_30_3_1.000000e-001_5_1_1.000000e-001.tif
The number I am interested to extract is the 1169. Sometimes this number can be longer or shorter. The property I can use is that always there are 4 character before it 'Seg_'
I tried to work it out with the sscanf() but I didn't have any success. Do you have any recommendation ?
Thank you in advance

채택된 답변

Image Analyst
Image Analyst 2012년 8월 26일
편집: Image Analyst 2012년 8월 26일
Just take the string between the first and second underline and pass it in to str2double(). Try this:
s = 'Seg_1169_Edgefun_1_5_2_15_90_30_3_1.000000e-001_5_1_1.000000e-001.tif'
underlineLocations = find(s == '_')
theNumber = str2double(s(underlineLocations(1)+1:underlineLocations(2)-1))
  댓글 수: 2
Dimitris M
Dimitris M 2012년 8월 26일
Perfect ! Thank you !
Jan
Jan 2012년 8월 27일
편집: Jan 2012년 8월 27일
strfind(s, '_') is faster than find(s == '_').

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

추가 답변 (1개)

Jan
Jan 2012년 8월 27일
The sscanf-method looks easy:
n = sscanf(s, 'Seg_%d');

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by