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]
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!