Not sure what I'm doing wrong. FOR LOOP

조회 수: 6 (최근 30일)
Derryn
Derryn 2013년 4월 3일
This is my code for a problem that must identify the start and end position of a genome.
% fid = fopen('sequence_short.txt','r'); %Opens the file for reading
new = fopen('report_short_codon.txt','w+'); %Opens the new file for writing
fprintf(new,'Name: Derryn Scott \n'); %Prints name, date, lab title
fprintf(new,'Date: March 28, 2013 \n');
fprintf(new,'Lab 10: DNA Pattern Matching\n');
C = textscan(fid,'%1s');
codons = C{1};
for t = 1:1:63 %Steps through each value
if strcmp('T' , codons{t});
if strcmp('A' , codons{t+1});
if strcmp('C' , codons{t+2})
StartCodon = t;
break
end
end
end
end
for t = StartCodon:1:63
if strcmp('A' , codons{t});
if strcmp('T' , codons{t+1});
if strcmp('T' , codons{t+2});
elseif strcmp('C' , codons{t+2});
EndCodon = t;
break
end
end
end
if strcmp('A' , codons{t});
if strcmp('C' , codons{t+1});
if strcmp('T' , codons{t+2});
EndCodon = t;
end
end
end
end
if fid == -1 %Prints an error if needed
error('The file has failed to open')
end
%Prints everything in the new file
fprintf(new,'Start Position is : %0.0d',StartCodon);
fprintf(new,'End Position is : %0.0d',EndCodon);
fclose(fid);
fclose(new);
I keep getting this error:
  댓글 수: 1
Derryn
Derryn 2013년 4월 3일
Index exceeds matrix dimensions.
Error in PROBLEM3Lab10 (line 20) if strcmp('T' , codons{t+1});

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

답변 (1개)

Cedric
Cedric 2013년 4월 3일
편집: Cedric 2013년 4월 3일
Difficult to know without seeing your sequence, but imagine that the cell array codons contains 60 characters.. when t = 60, addressing the element t+1 generates the error that you get. Also, realize that this code will crash if the start codon is not found.
You should display t for debugging, and also length(codons).
Now about the overall approach: you could read the file using FILEREAD and you would get an array of characters instead of this "complicated cell array". Then you could use STRFIND to identify positions of the codons that you want to match.
Another approach could be based on REGEXP for pattern matching.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by