extractBefore function not giving correct answer.

조회 수: 1 (최근 30일)
amna chaudhary
amna chaudhary 2018년 4월 10일
댓글: amna chaudhary 2018년 4월 10일
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.

채택된 답변

Birdman
Birdman 2018년 4월 10일
편집: Birdman 2018년 4월 10일
The following code simply does what you want:
seq = 'MSKAHFRQWTYVSKATYQRW';
for i=1:numel(seq)
Seq{i}=seq(1:i);
end
The outputs are stored in a cell array, you can see any fragment by typing
Seq{1}
Seq{2}
and so on.
  댓글 수: 1
amna chaudhary
amna chaudhary 2018년 4월 10일
Thanks a lot. God bless you abundantly. Was stuck on this for quite a long time now

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 4월 10일
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

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by