Convert single string with many numbers to vector

29 visualizaciones (últimos 30 días)
Pati Stan
Pati Stan el 31 de Jul. de 2019
Comentada: Stephen23 el 7 de Ag. de 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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (3)

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

Iniciar sesión para comentar.


Stephen23
Stephen23 el 1 de Ag. de 2019
Editada: Stephen23 el 1 de Ag. de 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 el 31 de Jul. de 2019
This worked beautifully! Thank you!

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by