필터 지우기
필터 지우기

EXTRACT NUMBER FROM A COMBINED STRING AND NUMBERS

조회 수: 1 (최근 30일)
Diego
Diego 2014년 6월 13일
댓글: Diego 2014년 6월 13일
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?

답변 (3개)

Dishant Arora
Dishant Arora 2014년 6월 13일
String = 'mario5.png';
output = String(regexp(String , '[0-9]'));
  댓글 수: 1
Diego
Diego 2014년 6월 13일
but what if the nomber is bigger than 10 will this work?

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


David Sanchez
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.

Brian B
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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by