Convert single string with many numbers to vector

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 日

0 投票

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 日

1 投票

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 日

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
Pati Stan
Pati Stan 2019 年 7 月 31 日

0 投票

This worked beautifully! Thank you!

カテゴリ

ヘルプ センター および File ExchangeSparse Matrices についてさらに検索

タグ

質問済み:

2019 年 7 月 31 日

コメント済み:

2019 年 8 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by