Extracting a number from a sentence string

I am trying to extract numbers from a sentence string through a function that does not know the exact placement or how many numbers are in the string.
for example my input would be
a='my weight is between 200 and 250 lbs'
or it could be
a='my weight is 200 lbs'
the weight will always be before "lbs", but the length of weight string is unknown.
I am having trouble using the sscanf function to make a universal code to extract the numbers from a string. Any help is greatly appreciated.

답변 (1개)

per isakson
per isakson 2015년 2월 8일
편집: per isakson 2015년 2월 8일

3 개 추천

A start
>> a='my weight is between 200 and 250 lbs'
a =
my weight is between 200 and 250 lbs
>> cac = regexp( a, '\d++', 'match' )
cac =
'200' '250'
Warning: This will not match '1.2E-003', not even '-4' :-(

카테고리

태그

질문:

2015년 2월 8일

편집:

2015년 2월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by