Filter löschen
Filter löschen

Converting strings in cell array into array

2 Ansichten (letzte 30 Tage)
SJ Won
SJ Won am 8 Jul. 2019
Kommentiert: SJ Won am 10 Jul. 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 :)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Jul. 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 Kommentar
SJ Won
SJ Won am 10 Jul. 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'.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by