Convert single string with many numbers to vector

30 vues (au cours des 30 derniers jours)
Pati Stan
Pati Stan le 31 Juil 2019
Commenté : Stephen23 le 7 Août 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!

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (3)

Fangjun Jiang
Fangjun Jiang le 31 Juil 2019
a=str2num(hue);
  2 commentaires
Pati Stan
Pati Stan le 31 Juil 2019
Also works great! Thanks!
Stephen23
Stephen23 le 1 Août 2019
Note that str2num relies on eval.

Connectez-vous pour commenter.


Stephen23
Stephen23 le 1 Août 2019
Modifié(e) : Stephen23 le 1 Août 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 le 31 Juil 2019
This worked beautifully! Thank you!

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by