matlab如何提取字符串中的数字

2025-01-01 20:58:44
推荐回答(2个)
回答1:

用正则表达式匹配吧:
>> s = '[数字一12000] [数字二28000]'

s =

[数字一12000] [数字二28000]

>> ms = regexp( s, '(?<=\w+)\d+', 'match' )

ms =

'12000' '28000'

然后将字符串转换为数字:
>> num1 = str2num( ms{ 1 } )

num1 =

12000

回答2:

a='[数字一12000] [数字二28000]';
index1=strfind(a,'一');
index2=strfind(a,'二');
index3=strfind(a,']');

num1=a(index1+1:index3(1)-1)
num2=a(index2+1:index3(2)-1)