How to split characters of a cell/string in matlab?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ivan Mich
el 3 de Mzo. de 2023
Comentada: Stephen23
el 4 de Mzo. de 2023
I have a question about a code. I have a string array and I would like to split it into pairs, depending the number of elements.
for example I have string 'CHARLES'. I would like to have a cell array with the elements C={CH, HA, AR, RL, LE, ES}. (I mean one cell array with 1x6 (length-1) dimensions)
my code is:
g='CHARLES'
length(g)
for i=1:(length(g)-1)
C=horzcat(g(i),g(i+1))
%charArray(i) = [AL(i){:}]
end
How must I modify my code? Could you please help me?
0 comentarios
Respuesta aceptada
Voss
el 3 de Mzo. de 2023
g='CHARLES';
C = cellstr([g(1:end-1); g(2:end)].').'
2 comentarios
Stephen23
el 4 de Mzo. de 2023
Use curly braces to access the content of cell arrays, not parentheses:
C = {'charles','hello','world'};
for k = 1:numel(C)
C{k} = cellstr([C{k}(1:end-1); C{k}(2:end)].').';
end
Checking the content:
C{:}
The ways to access cell arrays are explained here:
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Identification 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!