フィルターのクリア

Extracting number value from string from string using sscanf()

3 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by