Sum of matrix elements after using regexp

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]

 Respuesta aceptada

Star Strider
Star Strider el 24 de En. de 2018
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

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 24 de En. de 2018

Respondida:

el 24 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by