Convert Cell to string

조회 수: 1,327 (최근 30일)
Robin
Robin 2013년 2월 23일
댓글: Stephen23 2024년 1월 3일
I have got a simple question. I want to convert a cell array to a string array, somehow I don't succeed in doing this.
An example:
I' ve got this: a={1650}, which is a cell array. Because I want to concatenate this with other strings I need to convert the cell array to a string array. I have tried many converting functions but I dont get the correct final string array.
Hopefully somebody can help me with this.
Many Thanks.

답변 (2개)

Jonathan Thomet
Jonathan Thomet 2020년 11월 12일
You have this in the documentation.
C = {'Li','Sanchez','Jones','Yang','Larson'}
B = string(C)
That should do the trick.
  댓글 수: 9
Walter Roberson
Walter Roberson 2024년 1월 2일
To avoid changing values during conversion, use compose instead of plain string . You will need %.754g for exact conversion of numeric values.
Floyd
Floyd 2024년 1월 2일
@Walter Roberson, I'll give this a go. I appreciate it

댓글을 달려면 로그인하십시오.


Youssef  Khmou
Youssef Khmou 2013년 2월 23일
hi, You can accomplish that by converting the cell to matrix first then coverting the matrix to string array
B=num2str(cell2mat(A));
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 11월 12일
B = cellfun(@val2str, A, 'uniform', 0);
function str = val2str(val)
str = evalc(disp(val));
end
Stephen23
Stephen23 2024년 1월 3일
Or without EVALC, since R2021a:
B = cellfun(@formattedDisplayText, A, 'uni', 0);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by