Splitting a cell array of multi-word strings into a cell array of single-word strings
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Illan Kramer
el 3 de Feb. de 2020
Comentada: Illan Kramer
el 3 de Feb. de 2020
I have a cell array of multi-word strings that is very long (many tens of thousands of cells) that I want to split into a cell array of single-word strings. Is there a way to do this without combining the split function and a for loop?
Currently, I am doing the following:
CellStrings = {'Here is my First String';'Now a second string';'And here is a third'}
SingleColumnStrings = {};
for i = 1:length(CellStrings)
temp = split(CellStrings(i));
SingleColumnStrings = [SingleColumnStrings; temp];
clear temp
end
clear i
When CellStrings gets large, this for loop takes forever. Is there a way to do this as a matrix/vector operation?
Thanks in advance.
1 comentario
Guillaume
el 3 de Feb. de 2020
Note that clear is rarely needed. clear inside a loop is a particularly bad idea and will slow the loop for no useful purpose.
Respuesta aceptada
fred ssemwogerere
el 3 de Feb. de 2020
I think this can do nicely:
SingleColumnStrings=cellstr(strsplit(strjoin(string({'Here is my First String';'Now a second string';'And here is a third'})'))')
1 comentario
Más respuestas (1)
Guillaume
el 3 de Feb. de 2020
Editada: Guillaume
el 3 de Feb. de 2020
Possiblty more efficient than the accepted answer since it doesn't require concatenating strings to then split them again:
SingleColumnStrings = regexp(CellStrings, '\S+', 'match');
SingleColumnStrings =[SingleColumnStrings{:}].';
2 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!