How to convert strings to a matrix
Mostrar comentarios más antiguos
I'm trying to convert strings to a matrix E.g Hello world would be [hello, world]
I have a .m file, and I'm able to read my strings but I don't know how to convert them. Can someone help me.
Respuesta aceptada
Más respuestas (3)
Image Analyst
el 21 de Nov. de 2012
They already are arrays, as I'm sure you probably just found out:
s = 'Hello World'
firstWord = s(1:5) % Extract elements of the array
Thomas
el 21 de Nov. de 2012
You could also try
in='Hello World'
splitstring = textscan(in,'%s');
out=splitstring{:}'
Jan
el 21 de Nov. de 2012
Note that matrices of type char are padded by spaces. The string 'Hello world' contains a space in the middle. Does this disappear in ['Hello'; 'world']? And if so, how do you want to control this explicitly?
Str = 'This is a sentencte with word separated by spaces';
CStr = regexp(Str, ' ', 'split');
% >> CStr = {'This'; 'is'; 'a'; ...}
CharMatrix = char(CStr);
% >> CharMatrix =
['This '; ...
'is '; ...
'a '; ...
'sentencse'; ...
Usually working with cell strings is more efficient.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!