Not sure what I'm doing wrong. FOR LOOP

12 vues (au cours des 30 derniers jours)
Derryn
Derryn le 3 Avr 2013
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 commentaire
Derryn
Derryn le 3 Avr 2013
Index exceeds matrix dimensions.
Error in PROBLEM3Lab10 (line 20) if strcmp('T' , codons{t+1});

Connectez-vous pour commenter.

Réponses (1)

Cedric
Cedric le 3 Avr 2013
Modifié(e) : Cedric le 3 Avr 2013
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.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by