How to get numbers from char array?

35 visualizaciones (últimos 30 días)
David Mabwa
David Mabwa el 10 de Ag. de 2020
Editada: Adam Danz el 10 de Ag. de 2020
I have an array of characters that are produced from a fitSVMposterior
'@(S)sigmoid(S,-1.588276e+00,-2.598747e-01)'
I need to access the numbers in this array but don't know how.
Might anyone know how to do so?
Thanks

Respuesta aceptada

Adam Danz
Adam Danz el 10 de Ag. de 2020
Editada: Adam Danz el 10 de Ag. de 2020
Assuming your string is named, str,
str = '@(S)sigmoid(S,-1.588276e+00,-2.598747e-01)';
numstr = regexp(str,'(-)?\d+(\.\d+)?(e(-|+)\d+)?','match')
% numstr =
% 1×2 cell array
% {'-1.588276e+00'} {'-2.598747e-01'}
to convert to double,
num = str2double(numstr)
% num =
% -1.5883 -0.25987
  1 comentario
David Mabwa
David Mabwa el 10 de Ag. de 2020
Worked like a charm. Thank you very much!

Iniciar sesión para comentar.

Más respuestas (1)

Shae Morgan
Shae Morgan el 10 de Ag. de 2020
Editada: Shae Morgan el 10 de Ag. de 2020
str2double(char_array)
  2 comentarios
Adam Danz
Adam Danz el 10 de Ag. de 2020
Editada: Adam Danz el 10 de Ag. de 2020
This will not work when non-numeric values are in the character array.
Otherwise, when the string contains only a numeric representation, this is a good solution.
Shae Morgan
Shae Morgan el 10 de Ag. de 2020
excellent point!
better solution below

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by