How to get numbers from char array?

조회 수: 43 (최근 30일)
David Mabwa
David Mabwa 2020년 8월 10일
편집: Adam Danz 2020년 8월 10일
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
David Mabwa
David Mabwa 2020년 8월 10일
Worked like a charm. Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (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