MATLAB Answers

How to convert a 1x1 cell to a string?

7.142 views (last 30 days)
How to convert a 1x1 cell like {'line'} to a character vector like 'line', or a string like "line" please. thx

  0 Comments

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Feb 2015
Edited: MathWorks Support Team on 27 Nov 2018
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)

  5 Comments

Show 2 older comments
Raman Shrestha
Raman Shrestha on 6 Dec 2018
it has done magic to me thanks a lot
Yusuf Selim KARATAS
Yusuf Selim KARATAS on 15 Feb 2019
Thanks Azzi Abdelmalek. Works perfect.
JUNHO KWEON
JUNHO KWEON on 25 Apr 2019
That's a piece of cake. Thanks a lot!

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 1 Feb 2015
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 on 14 Jun 2019
Even if you have more than 1 string in our cell array, an easy way can be:
S = {'Hello',' ','world'}
ss = [S{:}]

  0 Comments

Sign in to comment.

Sign in to answer this question.