Converting strings in cell array into array

1 visualizzazione (ultimi 30 giorni)
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 :)

Risposta accettata

Walter Roberson
Walter Roberson il 8 Lug 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 Commento
SJ Won
SJ Won il 10 Lug 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'.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by