cell array to string array

fp31 ='#CC' '#CB' '#CN' '#CO' '#CP' '#CF' '#CS' '#CI' '#CQ' '#CW'
i have a cell array like this .. i want to convert every single cell of the array to string.
fp31(1)='#CC' should be string.

Risposte (3)

Walter Roberson
Walter Roberson il 13 Giu 2018
In R2016b or later, you can do
fp31 = string({'#CC','#CB','#CN','#CO','#CP','#CF','#CS','#CI','#CQ','#CW'});
and then fp31(1) would be the scalar string "#CC" as a string object. The corresponding character vector would be fp31{1}

5 Commenti

Jürgen
Jürgen il 31 Mar 2021
Modificato: Image Analyst il 1 Apr 2021
This is the information that is the helpful for most searchers.
It would be helpful to get this on top of all the Answers.
When the Question was originally asked, matlab did not have string objects, but row vectors of characters were commonly referred to as strings.
Jurgen, if an Answer was "Accepted" by the original poster, it will be at the top. Otherwise, the Answers with the most "Votes" will be at the top. You can vote for the answer if you want to lift it upward.
I can't Accept my own answer ;-)
Well at least I clicked the Vote icon to award you reputation points.

Accedi per commentare.

David Sanchez
David Sanchez il 11 Lug 2014
If your cell array is this:
fp31={'#CC','#CB','#CN','#CO','#CP','#CF','#CS','#CI','#CQ','#CW'};
access the elements using brackets:
fp31{1}
ans =
#CC
which will be a string array

4 Commenti

Kishore
Kishore il 12 Lug 2014
how to assign all these converted strings into another variable using some for loop ??
i want to take that string array and compare with predefined format i have,for some calculations
thanks
Huh?
fp31={'#CC','#CB','#CN';'#CO','#CP','#CF';'#CS','#CI','#CQ'};
f33a = fp31{3,3}
whos f33a
f33b = fp31{3}{3}
whos f33b
You see
f33a =
'#CQ'
Name Size Bytes Class Attributes
f33a 1x3 6 char
Brace indexing is not supported for variables of this type.
Error in test2 (line 4)
f33b = fp31{3}{3}
in R2018a.
Stephen23
Stephen23 il 13 Giu 2018
"Since it's related and took me awhile to realize, if the cell array is multi-dimensional like this:"
What your apparent "solution" shows is totally unrelated to whether a cell array is "multi-dimensional" or not, but is solely due to the fact that you have nested cell arrays. If you have actually tried your example multi-dimensional cell array (which does not contain nested cell arrays) with your suggested "solution" then you will find that you get an error (as Image Analyst has already shown). There is no reason why subscript indexing cannot be used to access the contents of a multi-dimensional cell array. If those cells happen to contain more cell arrays, then of course more cell array indexing will be required. And this is true for a scalar cell array, a vector cell array, or even a multi-dimensional cell array: it is unrelated to whether the cell array is multi-dimensional or not.
Jeff Bush
Jeff Bush il 13 Giu 2018
@Stephen (and Image Analyst) - everything you said is spot on, I was clueless I had nested cell arrays. I deleted my comment since like you said, it's not related at all. Sorry for being retarded.

Accedi per commentare.

Image Analyst
Image Analyst il 12 Lug 2014
Try this:
fp31={'#CC','#CB','#CN','#CO','#CP','#CF','#CS','#CI','#CQ','#CW'}
c = char(fp31) % Create 2D character array.
If some of the strings in fp31 are longer than others I think it pads with spaces so that the array is rectangular, as all matrices must be.
That's how to convert to "strings", but like David said every single cell contains a string and you can get to it without doing anything, so we don't know why you're asking what you asked. I think the FAQ will give you a good intuitive feel for how cell arrays work: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

5 Commenti

i actually have a string like..
'#CB#CN#CO#CP#CF#CS'
for some purpose i have cut it down it into 3 characters a string using REGEXP it gave me cells of a cell array which i was not intend to get .
what now i want to do is convert every single cell into string and to perform some operations on every string
Image Analyst
Image Analyst il 12 Lug 2014
My answer remains the same.
Kishore
Kishore il 12 Lug 2014
Modificato: Kishore il 12 Lug 2014
how to assign all these converted strings into another variable using some loop ??
i want to take that string array and compare with predefined format i have,for some calculations
Image Analyst
Image Analyst il 12 Lug 2014
Can you just use ismember. Maybe you can adapt my answer here: http://www.mathworks.com/matlabcentral/answers/141451#answer_144783
Starting with r2017b, there is also a convertCharsToStrings() function that people may want to know about. From the help:
Convert a cell array of character vectors to a string array.
C = {'Venus','Earth','Mars'}
C = 1x3 cell
{'Venus'} {'Earth'} {'Mars'}
str = convertCharsToStrings(C)
str = 1x3 string
"Venus" "Earth" "Mars"

Accedi per commentare.

Categorie

Prodotti

Richiesto:

il 11 Lug 2014

Commentato:

il 1 Apr 2021

Community Treasure Hunt

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

Start Hunting!

Translated by