How to get numbers from char array?

53 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by