MATLAB Answers

1

How to convert a 1x1 cell to a string?

Asked by bh dhouha on 1 Feb 2015
Latest activity Commented on by Yusuf Selim KARATAS on 15 Feb 2019
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.

2 Answers

Answer by Azzi Abdelmalek
on 1 Feb 2015
Edited by MathWorks Support Team on 27 Nov 2018
 Accepted Answer

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)

  4 Comments

Show 1 older comment
If you are using a recent version of MATLAB you may find that a string or string array is more convenient then the character array created in the above example and by the char function:
>> string({'one'})
ans =
"one"
>> string({'one','two'})
ans =
1×2 string array
"one" "two"
it has done magic to me thanks a lot
Thanks Azzi Abdelmalek. Works perfect.

Sign in to comment.


Answer by 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.

  1 Comment

Sign in to comment.