How to convert a 1x1 cell to a string?

조회 수: 4,153 (최근 30일)
bh dhouha
bh dhouha 2015년 2월 1일
댓글: Paula Camila 2023년 2월 19일
How to convert a 1x1 cell like {'line'} to a character vector like 'line', or a string like "line" please. thx

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 2월 1일
편집: MathWorks Support Team 2020년 9월 3일
To convert a cell array of character vectors to a character array, use the “char” function.
A = {'line'}
B = char(A)
To extract the contents from a cell, index using curly braces.
A = {'line'}
B = A{1}
Starting in R2016b, you can store text in string arrays. To convert a cell array to a string array, use the “string” function.
A = {'line'}
B = string(A)
For more information, please refer to the MathWorks documentation on Cell Arrays of Character Vectors and Text in String and Charater Arrays.
  댓글 수: 6
Junho Kweon
Junho Kweon 2019년 4월 25일
That's a piece of cake. Thanks a lot!
Paula Camila
Paula Camila 2023년 2월 19일
gracias

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

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 2월 1일
Azzi showed you how to extract the string from a cell. Another way is to convert the cell with char():
ca={'line'} % This is our cell array.
str = char(ca) % Convert it to a character array (string).
Net, both give the same result, just different ways of getting there. If your cell array is only a single solitary cell, then you should not even use a cell in the first place - use a string.

Morteza Darvish Morshedi
Morteza Darvish Morshedi 2019년 6월 14일
Even if you have more than 1 string in our cell array, an easy way can be:
S = {'Hello',' ','world'}
ss = [S{:}]
  댓글 수: 5
Image Analyst
Image Analyst 2021년 2월 15일
Just a point of clarification. ss is a character array while str is a string. A few versions ago, MATLAB made these different types of variables, though in common speech, people call either one "strings".
Morteza Darvish Morshedi
Morteza Darvish Morshedi 2021년 2월 20일
Right. Thanks.

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

카테고리

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