extractBefore function not giving correct answer.
1 view (last 30 days)
Show older comments
amna chaudhary
on 10 Apr 2018
Commented: amna chaudhary
on 10 Apr 2018
I have a protein sequence and I want to make fragments of it. for example the protein sequence is 'MSKAHFRQWTYVSKATYQRW' and the fragments should be 'M', 'MS', 'MSK', 'MSKA', 'MSKAH', 'MSKAHF', 'MSKAHFR', 'MSKAHFRQ' and so on. Im using the following code:
seq = 'MSKAHFRQWTYVSKATYQRW';
frag = [];
for i = 1:length(seq)
ii = extractBefore (seq, seq(i));
frag = [frag char (length(seq)) ii];
end
frag
But after some loops it does not follow the rule and starts giving wrong answers. The output is :
'M MS MSK MSKA MSKAH MSKAHF MSKAHFR MSKAHFRQ MSKAHFRQW MSKAHFRQWT MSKAHFRQWTY M MS MSK MSKAHFRQW MSKAHFRQWT MSKAHFR MSKAHF MSKAHFRQ'
please help. TIA.
0 Comments
Accepted Answer
More Answers (1)
Steven Lord
on 10 Apr 2018
So at iteration n you want the first n characters from the text? If you're using a char vector, just use indexing.
word = 'hello';
for k = 1:length(word)
fprintf('%s\n', word(1:k));
end
If you're using a string, use extractBetween.
word = "hello";
for k = 1:strlength(word)
fprintf('%s\n', extractBetween(word, 1, k));
end
See Also
Categories
Find more on Language Support 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!