how to convert a string to a vector

Hi I have a string chain and wish to convert it into a vector So if my input is: x =1,3,3,4,5,6,6 I need an output
y = [1 3 3 4 5 6 6] How do I do this?

Respuestas (1)

per isakson
per isakson el 1 de Dic. de 2015
Editada: per isakson el 1 de Dic. de 2015
One way
>> str ='1,3,3,4,5,6,6'
str =
1,3,3,4,5,6,6
>> num = textscan( str, '%f', 'Delimiter',',' )
num =
[7x1 double]
and together with the missing step
str ='1,3,3,4,5,6,6';
num = textscan( str, '%f', 'Delimiter',',' );
num = permute( num{1}, [2,1] )
num =
1 3 3 4 5 6 6
another way
>> str2num( str )
ans =
1 3 3 4 5 6 6

7 comentarios

lamghari
lamghari el 1 de Dic. de 2015
I have the result in the form of columns
Walter Roberson
Walter Roberson el 2 de Dic. de 2015
Are you indicating that your input is a string that has newlines and words like "Columns 326 through 338" inside it, or are you discussing what the output looks like after textscan or str2num?
If your input is a string like that, "Don't do that", get access to the original data instead.
lamghari
lamghari el 3 de Dic. de 2015
Editada: per isakson el 3 de Dic. de 2015
No, i am discussing what the output looks like after str2num my input is for example:
6,7,6,6,6,6,6,5,4,4,4,4,3,4,4,4,4,4,5,5,4,5,5,5,6,5,6,6,6,6,6,5,5,4,4,5,4,4,5,4,4,4,4,4,4,4,4,3,4,4,3,6,6,1,0,0,7,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,7,6,7,7,6,5,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1,2,1,2,2,3,2,2,3,3,3,4,3,2,2,2,2,2,3,2
lamghari
lamghari el 3 de Dic. de 2015
Editada: per isakson el 3 de Dic. de 2015
i need an output:
y = [6 7 6 6 6 6 6 5 4 4 4 4 3 4 4 4 4 4 5 5 4 5 5 5 6 5 6 6 6 6 6 5 5 4 4 5 4 4 5 4 4 4 4 4 4 4 4 3 4 4 3 6 6 1 0 0 7 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 7 6 7 7 6 5 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 1 1 2 1 2 2 3 2 2 3 3 3 4 3 2 2 2 2 2 3 2]
"How do I do this?" &nbsp The short answer is: You cannot do that. And that's because
y = [6 7 6 6 6 6 6 5 4 4 4 4 ... ]
isn't a display format. It's an example of an assignment of a numerical vector to a variable, y
lamghari
lamghari el 8 de Dic. de 2015
ok.thank you very much
output = ['y = [', regexprep(num, ',', ' '), ']' ];
If what you want is the string 'y = [6 7 6 6 6 6 6 5 4 4 4 4 ... ]'

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Dic. de 2015

Comentada:

el 8 de Dic. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by