How to make this string a = '(0 0 0)' into a double b = [0 0 0]?

1 回表示 (過去 30 日間)
Wictor Oliveira
Wictor Oliveira 2022 年 8 月 5 日
コメント済み: Walter Roberson 2022 年 8 月 5 日
Suppose I don't know what are the number inside the a string, it could be:
a = '(12 2.8 1.22)' % each number is separated by a single space
which should then be:
b = [12 2.8 1.22]

採用された回答

Kevin Holly
Kevin Holly 2022 年 8 月 5 日
a = '(12 2.8 1.22)'
a = '(12 2.8 1.22)'
a = strrep(a,')',']');
a = strrep(a,'(','[');
b = str2num(a)
b = 1×3
12.0000 2.8000 1.2200

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2022 年 8 月 5 日
編集済み: Fangjun Jiang 2022 年 8 月 5 日
a = '(12 2.8 1.22)';
b=sscanf(a,'(%f %f %f)')
b = 3×1
12.0000 2.8000 1.2200
b=transpose(sscanf(a,'(%f %f %f)'))
b = 1×3
12.0000 2.8000 1.2200
  2 件のコメント
Wictor Oliveira
Wictor Oliveira 2022 年 8 月 5 日
Great!!! Thank you very much!!!
Walter Roberson
Walter Roberson 2022 年 8 月 5 日
This is more robust.

サインインしてコメントする。

カテゴリ

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