Filter löschen
Filter löschen

EXTRACT NUMBER FROM A COMBINED STRING AND NUMBERS

1 Ansicht (letzte 30 Tage)
Diego
Diego am 13 Jun. 2014
Kommentiert: Diego am 13 Jun. 2014
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?

Antworten (3)

Dishant Arora
Dishant Arora am 13 Jun. 2014
String = 'mario5.png';
output = String(regexp(String , '[0-9]'));
  1 Kommentar
Diego
Diego am 13 Jun. 2014
but what if the nomber is bigger than 10 will this work?

Melden Sie sich an, um zu kommentieren.


David Sanchez
David Sanchez am 13 Jun. 2014
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 am 13 Jun. 2014
Bearbeitet: Brian B am 13 Jun. 2014
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'))

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by