Not sure what I'm doing wrong. FOR LOOP

6 次查看(过去 30 天)
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