EXTRACT NUMBER FROM A COMBINED STRING AND NUMBERS
1 回表示 (過去 30 日間)
古いコメントを表示
I have for example the following string 'mario5.png' or 'Julian15.png' and want to extract the numbers from there in the first case 5 in the second case 15. Can anyone tell me which function to look for?
0 件のコメント
回答 (3 件)
David Sanchez
2014 年 6 月 13 日
use str2double to get it in double format:
output = str2double(String(regexp(String , '[0-9]')))
output =
5
otherwise your output will be a string.
0 件のコメント
Brian B
2014 年 6 月 13 日
編集済み: Brian B
2014 年 6 月 13 日
If the number is bigger than 9, you can make a slight modification to the answer by David Sanchez:
output = str2double(String(regexp(String , '[0-9]+')))
This will work for positive integers. The "+" means "one or more". You can also skip a step by requesting that regexp return the matching part of the string instead of the index of the match:
output = str2double(regexp(String , '[0-9]+', 'match'))
参考
カテゴリ
Help Center および File Exchange で String についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!