MATLAB: String not read correctly

string read

In the code below, for b = 'AGA' the ouput is c = 'S'. Why isn't the output c = 'R'?
b = 'AGA';
if b=='UUU' | b=='UUC'
c = 'F'
elseif b =='UCU' | b=='UCC' | b=='UCA' | b=='UCG' | b=='AGC' | b=='AGU'
c = 'S'
elseif b =='CGU' | b=='CGC' | b=='CGA' | b=='CGG' | b=='AGA' | b=='AGG'
c = 'R'
end

Best Answer

Explanation
"...the ouput is c = 'S'. Why isn't the output c = 'R'?"
Because your code does not do what you think it does.
The reason why your code does not work is explained in the documentation, which is why you should always read the documentation for every operation and function that you use. Lets have a look at why your code does not work. The reason why c = 'S' is returned is clearly because the elements of the first elseif condition are all true, exactly as the if documentation states. Lets have a look at the if condition to check:
>> b = 'AGA';
>> b =='UCU' | b=='UCC' | b=='UCA' | b=='UCG' | b=='AGC' | b=='AGU'
ans =
1 1 1
And the reason why the elements are all true is because you decided to use the logical or |, which works element-wise on arrays. In fact only two of the terms are required to get this effect:
>> b=='UCA' | b=='AGC'
ans =
1 1 1
because each of these terms separately gives this:
>> b=='UCA'
ans =
0 0 1
>> b=='AGC'
ans =
1 1 0
and clearly the or of those two vectors is one vector with all true, and thus the first elseif contains only true, and so c = 'S'.
Basically you used the wrong commands, did not bother to read the documentation, and did not check the output from the code that you wrote. These are common mistakes that beginners make: they rely on what they imagine/want/wish/believe their code is doing, and never actually check what the code is really doing. Code cannot read your mind, and does not care about what you want. It is your job to read the documentation and check each line of code to ensure that it is doing exactly what you want.
Better Solutions
MATLAB is a beautiful high-level language, so please do not write ugly code as if this was an ugly low-level language like C++, where everything has to be solved using loops and ifs. As Walter Roberson stated, you should really use strcmp and short-circuit logical operators, or simply use ismember:
b = 'AGA';
if ismember(b,{'UUU','UUC'})
c = 'F';
...
end
or even simpler would be to use switch:
b = 'AGA';
switch b
case {'UUU','UUC'}
c = 'F';
...
end