Convert single string with many numbers to vector

6 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

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

Accedi per commentare.

Più risposte (3)

Fangjun Jiang
Fangjun Jiang il 31 Lug 2019
a=str2num(hue);

Stephen23
Stephen23 il 1 Ago 2019
Modificato: Stephen23 il 1 Ago 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 il 31 Lug 2019
This worked beautifully! Thank you!

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by