Convert single string with many numbers to vector

29 ビュー (過去 30 日間)
Pati Stan
Pati Stan 2019 年 7 月 31 日
コメント済み: Stephen23 2019 年 8 月 7 日
I have the following string
hue = '10 20 30 40 50';
That I want to turn into a vector of those 5 values
hue_vec = [10 20 30 40 50];
How can I do this? Thanks in advance!

採用された回答

madhan ravi
madhan ravi 2019 年 7 月 31 日
hue_vec = str2double(regexp(hue,'\d+','match'))
  1 件のコメント
madhan ravi
madhan ravi 2019 年 7 月 31 日
If you have decimals then the expressions would be:
hue_vec = str2double(regexp(hue,'\d*[\.]?\d*','match'))

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

その他の回答 (3 件)

Fangjun Jiang
Fangjun Jiang 2019 年 7 月 31 日
a=str2num(hue);
  2 件のコメント
Pati Stan
Pati Stan 2019 年 7 月 31 日
Also works great! Thanks!
Stephen23
Stephen23 2019 年 8 月 1 日
Note that str2num relies on eval.

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


Stephen23
Stephen23 2019 年 8 月 1 日
編集済み: Stephen23 2019 年 8 月 1 日
Very simple, very efficient, no evil eval:
>> hue = '10 20 30 40 50';
>> vec = sscanf(hue,'%f',[1,Inf])
vec =
10 20 30 40 50
  2 件のコメント
Pati Stan
Pati Stan 2019 年 8 月 1 日
Thanks! Can you elborate on what you mean by eval and why it's disadvantageous?

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


Pati Stan
Pati Stan 2019 年 7 月 31 日
This worked beautifully! Thank you!

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by