You can get the string out of a cell with the curly braces ({}
):
x='AGCT';
y(1) = {x};
y{1}
ans =
AGCT
And you can string together indexing operators to get individual characters directly from the cell array. For example:
y{1}(2)
ans =
G
Also keep in mind that the char
function can convert a cell array of strings into a 2D character array by vertically concatenating the strings while padding with white space as necessary:
S = char(C)
, when C
is a cell array of strings, places each element of C
into the rows of the character array S
. Use CELLSTR
to convert back.
This way you could convert your entire cell array into a 2D character array with just char(y)
, but I think you are looking for a way to do the indexing of individual characters directly from the cell array as above.
And speaking of cell array conversion, have a look at cellfun
, which can be used to perform the same operation on each cell. For example, if you had a cell such as y = {'AGCT','CTGA'};
and you wanted the second character of each cell (a character array containing GT
), you might be tempted to do y{:}(2)
, but this does not work (the first index must be a scalar). A solution is:
>> iBase = 2;
>> basei = cellfun(@(c)c(iBase),y)
basei =
GT