How to convert a 1x1 cell to a string?
Show older comments
How to convert a 1x1 cell like {'line'} to a character vector like 'line', or a string like "line" please. thx
Accepted Answer
More Answers (2)
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
Image Analyst
on 1 Feb 2015
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{:}]
5 Comments
Nisha Satia
on 15 Feb 2021
How to covert cell into string and delimited with space to store into matrix
Image Analyst
on 15 Feb 2021
Is this what you mean?
ca = {'Hello',' ','world'} % Cell Array.
charArray = [ca{:}]
str = convertCharsToStrings(charArray)
If not, start a new question and give your starting cell, and expected output.
Morteza Darvish Morshedi
on 15 Feb 2021
Maybe you are looking for this:
S = {'Hello','World'};
s2 = cell(1,length(S)); s2(:)={' '};
ss = [S;s2];
ss = [ss{:}]
Image Analyst
on 15 Feb 2021
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
on 20 Feb 2021
Right. Thanks.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!