Convert single string with many numbers to vector

31 次查看(过去 30 天)
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);

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

Pati Stan
Pati Stan 2019-7-31
This worked beautifully! Thank you!

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by