Converting strings in cell array into array

1 visualización (últimos 30 días)
SJ Won
SJ Won el 8 de Jul. de 2019
Comentada: SJ Won el 10 de Jul. de 2019
It reads a string of characters from the cell array in its column, then goes to the next column and reads that. It puts all that into one string of an array.
I solved the problem for the most part but it ignores spacing for some reason and I have no idea why.
y='';
for i=1:numel(x)
y = strcat(y, x{i})
end
For example, {'hello ' ; 'yes'} would have an output as 'helloyes', instead of 'hello yes'. How do I include that spacing as well?
Since it didn't work, I thought perhaps it was the cell array nuance that didn't let the spacing go through, so I tried using char:
y ='';
for i=1:numel(x)
y = strcat( y,char(x{i}))
end
but the result is the same.
Could someone explain why it ignores the spacing? Could I manipulate the code a little for it to not ignore the spacing, or do I have to change the entire code?
Thanks in advance :)

Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jul. de 2019
strcat() has the property that it trims out leading and trailing blanks from parameters that are character vectors. It does not do that for cell array of character vectors. For example, strcat('a', ' b ', 'c') and strcat('a', {' b '}, 'c') will produce different results.
  1 comentario
SJ Won
SJ Won el 10 de Jul. de 2019
Thank you, I solved it now. But after testing those two lines, it seems that strcat() only trims out the trailing blanks?
strcat('a', ' b ', 'c') outputs 'a bc', instead of 'abc'.

Iniciar sesión para comentar.

Más respuestas (0)

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!

Translated by