How to get numbers from char array?

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

 Accepted Answer

Adam Danz
Adam Danz on 10 Aug 2020
Edited: Adam Danz on 10 Aug 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

More Answers (1)

Shae Morgan
Shae Morgan on 10 Aug 2020
Edited: Shae Morgan on 10 Aug 2020
str2double(char_array)

2 Comments

Adam Danz
Adam Danz on 10 Aug 2020
Edited: Adam Danz on 10 Aug 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.
excellent point!
better solution below

Sign in to comment.

Categories

Products

Release

R2020a

Asked:

on 10 Aug 2020

Edited:

on 10 Aug 2020

Community Treasure Hunt

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

Start Hunting!