Splitting a 1 x1 cell array every three characters?
1 view (last 30 days)
Show older comments
I have a 1 x 1 cell array that is a short DNA codon sequence. I want to turn this sequence into a matrix with each column containing a single codon. How can I split this long string every three characters, and make it into a matrix?
0 Comments
Answers (2)
Image Analyst
on 13 Oct 2015
A 1x1 cell array is not much of an array - there's just one element! Anyway, try this:
stringInside = ca{1}; % Extract string from inside the cell array.
numChars = length(stringInside) % Get number of characters in the string. Must be a multiple of 3
% Reshape into a 3 column character array.
charArray = reshape(stringInside, numChars/3, 3)
0 Comments
Star Strider
on 13 Oct 2015
This is how I would do it:
B = {'A' 'T' 'C' 'G'}; % Bases
I = randi(4, 1, 12); % Sequence Index
S = {B(I)}; % Linear Sequence
S{:} % Display (Optional)
CM = reshape(S{:}, 3, []) % Codons In Columns
ans =
'G' 'G' 'G' 'A' 'T' 'A' 'A' 'T' 'C' 'C' 'C' 'C'
CM =
'G' 'A' 'A' 'C'
'G' 'T' 'T' 'C'
'G' 'A' 'C' 'C'
This is random, so running it will give different results each time, but the codons will match the order of those in the linear sequence.
0 Comments
See Also
Categories
Find more on Matrices and Arrays 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!