Converting strings in cell array into array

조회 수: 1 (최근 30일)
SJ Won
SJ Won 2019년 7월 8일
댓글: SJ Won 2019년 7월 10일
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