Converting strings in cell array into array

2 次查看(过去 30 天)
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 :)

采纳的回答

Walter Roberson
Walter Roberson 2019-7-8
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 个评论
SJ Won
SJ Won 2019-7-10
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'.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by