Sum of matrix elements after using regexp

1 次查看(过去 30 天)
I wrote the following code to sum all number after locating numeric values from a string by using regexp.
s='2 groupd 40cell f'
g =regexp(s,'\d*','match')
g = cell2mat(g)
It gives an incorrect result: 240 --> instead of [2 40]

采纳的回答

Star Strider
Star Strider 2018-1-24
The numbers are strings, so cell2mat will concatenate them into one string array.
The solution is to use the str2double function if you want them as separate numbers:
g = str2double(g)
g =
2 40

更多回答(0 个)

类别

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