Function Error / If and elseif statement help
이전 댓글 표시
Hello,
I have been trying to create this function to call when entering nucleotides and then having the string divided by 3 and then read and changed into the appropriate amino acid. When I try to run the function it comes up with the error:
'Not enough input arguements'
If anyone could help to fix this code up it would be amazing!!
function [amino_acid_chain] = synthesize2(neucleotide_string)
%Function to synthesize an amino acid chain from an mRNA molecule.
neucleotide_string = upper(neucleotide_string);
%Loop to check for invalid characters in neucleotide string.
while any(neucleotide_string ~= 'A' && neucleotide_string ~= 'G' && neucleotide_string ~= 'U' && neucleotide_string ~= 'C');
error('Error! Neucleotide string contains invalid characters.');
end
amino_acid_chain = cellstr(reshape(neucleotide_string,3,[])');
if length(amino_acid_chain)<3
amino_acid_chain = char([]);
return;
end
if amino_acid_chain == 'UUU' or 'UUC'
amino_acid_chain = replace(word,{'UUU','UUC'},{'F','F'});
elseif amino_acid_chain == 'UUA' or 'UUG' or 'CUU' or 'CUA' or 'CUG'
amino_acid_chain = replace(word,{'UUA','UUG','CUU','CUC','CUA','CUG'},{'L','L','L','L','L','L'});
elseif amino_acid_chain == 'AUU' or 'AUC' or 'AUA'
amino_acid_chain = replace(word,{'AUU', 'AUC', 'AUA'},{'I','I','I'});
elseif amino_acid_chain == 'AUG'
amino_acid_chain = replace(word,{'AUG'},{'M'});
elseif amino_acid_chain == 'GUU' or 'GUC' or 'GUA' or 'GUG'
amino_acid_chain = replace(word,{'GUU','GUC','GUA','GUG'},{'V','V','V','V'});
elseif amino_acid_chain == 'UCU' or 'UCC' or 'UCA' or 'UCG'
amino_acid_chain = replace(word,{'UCU','UCC','UCA','UCG'},{'S','S','S','S'});
elseif amino_acid_chain == 'CCU' or 'CCC' or 'CCA' or 'CCG'
amino_acid_chain = replace(word,{'CCU','CCC','CCA','CCG'},{'P','P','P','P'});
elseif amino_acid_chain == 'ACU' or 'ACC' or 'ACA' or 'ACG'
amino_acid_chain = replace(word,{'ACU','ACC','ACA','ACG'},{'T','T','T','T'});
elseif amino_acid_chain == 'GCU' or 'GCC' or 'GCA' or 'GCG'
amino_acid_chain = replace(word,{'GCU','GCC','GCA','GCG'},{'A','A','A','A'});
elseif amino_acid_chain == 'UAU' or 'UAC'
amino_acid_chain = replace(word,{'UAU','UAC'},{'Y','Y'});
elseif amino_acid_chain == 'CAA' or 'CAG'
amino_acid_chain = replace(word,{'CAA','CAG'},{'Q','Q'});
elseif amino_acid_chain == 'AAU' or 'AAC'
amino_acid_chain = replace(word,{'AAU','AAC'},{'N','N'});
elseif amino_acid_chain == 'AAA' or 'AAG'
amino_acid_chain = replace(word,{'AAA','AAG'},{'K','K'});
elseif amino_acid_chain == 'GAU' or 'GAC'
amino_acid_chain = replace(word,{'GAU','GAC'},{'D','D'});
elseif amino_acid_chain == 'GAA' or 'GAG'
amino_acid_chain = replace(word,{'GAA','GAG'},{'E','E'});
elseif amino_acid_chain == 'UGU' or 'UGC'
amino_acid_chain = replace(word,{'UGU','UGC'},{'C','C'});
elseif amino_acid_chain == 'UGG'
amino_acid_chain = replace(word,{'UGG'},{'W'});
elseif amino_acid_chain == 'CGU' or 'CGC' or 'CGA' or 'CGG'
amino_acid_chain = replace(word,{'CGU','CGC','CGA','CGG'},{'R','R','R','R'});
elseif amino_acid_chain == 'AGU' or 'AGC'
amino_acid_chain = replace(word,{'AGU','AGC'},{'S','S'});
elseif amino_acid_chain == 'AGA' or 'AGG'
amino_acid_chain = replace(word,{'AGA','AGG'},{'R','R'});
elseif amino_acid_chain == 'CGU' or 'GGC' or 'GGA' or 'GGG'
amino_acid_chain = replace(word,{'GGU','GGC','GGA','GGG'},{'G','G','G','G'});
elseif amino_acid_chain == 'UAA' or 'UAG' or 'UGA'
amino_acid_chain = replace(word,{'UAA','UAG','UGA'},{'Stop','Stop','Stop'});
end
end
I was also using 'or' in my if/elseif statement, however I don't think that is correct either...
댓글 수: 3
Rik
2020년 5월 4일
A few questions and some recommendations:
How are you calling this function?
If you are not sure you are using the or function correctly, why didn't you check the documentation?
The mlint is giving you several warnings. I would suggest you fix those.
If you find yourself using a lot of elseif statements, consider using switch, case, otherwise instead.
If you compare something with ==, it will be an element-wise operation. That means that 'test'=='foo' is not going to return false, it is going to give an error. There is no letter in foo that you can compare with the second t in test. If you want to compare strings: strcmp.
Samantha Pellegrino
2020년 5월 4일
편집: Rik
2020년 5월 4일
Rik
2020년 5월 4일
Why do you still have this?
if amino_acid_chain == 'Stop'
The goal of that if is unclear anyway, as there is no code between the return and the end of the function.
Also, have you seen my answer?
채택된 답변
추가 답변 (1개)
Steven Lord
2020년 5월 5일
편집: Steven Lord
2020년 5월 5일
Consider using a switch / case statement.
food = ["apple", "beans", "cauliflower", "dragonfruit", "egg"];
groceries = food(randi(numel(food), 20, 1)); % 20 random items from food
for whichItem = 1:numel(groceries)
item = groceries(whichItem);
switch item
case {"apple"; "dragonfruit"}
itIsA = "fruit";
case {"beans"; "cauliflower"}
itIsA = "vegetable";
otherwise
itIsA = "something else";
end
fprintf("Item %d, %s, is a %s.\n", whichItem, item, itIsA);
end
카테고리
도움말 센터 및 File Exchange에서 Protein and Amino Acid Sequence Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!