MATLAB: How to add leading zero to a letter in matlab

chardnastring

I have written a code which will accepts a sequence and convert it to specific numerical sequence. The code is given below–
if true
% code
end
function[xA]=dnatobinary(sample_dna)
for i=1:length(sample_dna)
if sample_dna(i)=='A'|| sample_dna='a'
xA(i)=01;
elseif sample_dna(i)=='T' || sample_dna='t'
xA(i)=11;
end
end
end
Now this code should convert 'ATTTA' into '0111111101'. Instead it is converting it as '11111111' i.e the leading zero for A=01 is missing.please provide appropriate coding.

Best Answer

It is much simpler and more efficient to use ismember or regexprep:
>> str = 'ATTTA';
>> regexprep(str,{'A','T'},{'01','11'},'ignorecase')
ans = 0111111101
or
>> [~,idx] = ismember(upper(str),'AT');
>> C = {'01','11'};
>> [C{idx}]
ans = 0111111101
You will simply waste a lot of time trying to replicate functionality that already exists.