How to get numbers from char array?

34 次查看(过去 30 天)
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

采纳的回答

Adam Danz
Adam Danz 2020-8-10
编辑:Adam Danz 2020-8-10
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 个)

Shae Morgan
Shae Morgan 2020-8-10
编辑:Shae Morgan 2020-8-10
str2double(char_array)
  2 个评论
Adam Danz
Adam Danz 2020-8-10
编辑:Adam Danz 2020-8-10
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 2020-8-10
excellent point!
better solution below

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by