Convert single string with many numbers to vector

29 Ansichten (letzte 30 Tage)
Pati Stan
Pati Stan am 31 Jul. 2019
Kommentiert: Stephen23 am 7 Aug. 2019
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!

Akzeptierte Antwort

madhan ravi
madhan ravi am 31 Jul. 2019
hue_vec = str2double(regexp(hue,'\d+','match'))
  1 Kommentar
madhan ravi
madhan ravi am 31 Jul. 2019
If you have decimals then the expressions would be:
hue_vec = str2double(regexp(hue,'\d*[\.]?\d*','match'))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Fangjun Jiang
Fangjun Jiang am 31 Jul. 2019
a=str2num(hue);
  2 Kommentare
Pati Stan
Pati Stan am 31 Jul. 2019
Also works great! Thanks!
Stephen23
Stephen23 am 1 Aug. 2019
Note that str2num relies on eval.

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 1 Aug. 2019
Bearbeitet: Stephen23 am 1 Aug. 2019
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 am 31 Jul. 2019
This worked beautifully! Thank you!

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by