How do I take my generated string outputs from for loop and return a list?
2 views (last 30 days)
Show older comments
seq = 'ACAUUUGCUUCUGACACAACUGUGUUCACUAGCAACCUCAAACAGACA'
for i=1:3:numel(seq)-2
a = (seq(i:i+2))
end
a = 'ACA'
a = 'UUU'
a = 'GCU'
a = 'GAC'
etc...
% Desired output --> ['ACA', 'UUU', 'GCU', 'GAC' ...]
0 Comments
Accepted Answer
Walter Roberson
on 19 Jan 2023
You should be careful what you ask for. 'ACA' is the 1 x 3 char vector ['A' 'C' 'A'] and ['ACA' 'UUU'] is concatenating two 1 x 3 vectors together getting a 1 x 6 vector 'ACAUUU' -- back to where you started
seq = 'ACAUUUGCUUCUGACACAACUGUGUUCACUAGCAACCUCAAACAGACA'
a1 = [];
a2 = cell(0);
for i=1:3:numel(seq)-2
a1 = [a1, seq(i:i+2)];
a2{end+1} = seq(i:i+2);
end
a1
a2
a3 = reshape(seq, 3, []).'
I would suggest to you that the a3 method is easiest. You might want to cellstr() or string() the resutling 16x3 array.
0 Comments
More Answers (1)
Sulaymon Eshkabilov
on 19 Jan 2023
Here si the solution:
a=[];
seq = 'ACAUUUGCUUCUGACACAACUGUGUUCACUAGCAACCUCAAACAGACA'
for i=1:3:numel(seq)-2
a = [a; (seq(i:i+2))];
end
a
See Also
Categories
Find more on Matrix Indexing 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!