count the number a letter appears in a string and plot a histogram

7 views (last 30 days)
I have to write a function that counts the number a base of DNA repeats itself inside of a string and then plot a histogram with the function 'histogram'. Here is my code to find the number of bases and it works fine
n=length(s);
if n>1000
disp('errore dimensione');
return
end
A=0;
C=0;
G=0;
T=0;
for i=1:n
switch s(i)
case 'A'
A=A+1;
case 'C'
C=C+1;
case 'G'
G=G+1;
case 'T'
T=T+1;
otherwise
disp('questo non è DNA');
return
end
end
vec=[A , C , G , T];
From here i want a histogram with x-axis representing the letters and y-axis representing the frequency each letter appears inside a string, how can i do that?

Answers (2)

Stephen23
Stephen23 on 18 Mar 2023
V = categorical(["A","C","G","T"]);
W = V(randi(4,1,23))
W = 1×23 categorical array
G T C C T T T T C A G C G G C T T A C A G A C
histogram(W)

DGM
DGM on 18 Mar 2023
Edited: DGM on 18 Mar 2023
I'm sure there are text analysis tools for this, but here's one basic way.
categories = 'ACGT';
% generate a test string
N = 20; % length of test string
teststr = categories(randi([1 numel(categories)],1,N))
teststr = 'GCTAGTATAAGGTTGAGAGG'
% find character frequencies
% if you just want character counts, don't divide
freq = sum(teststr == categories.',2)/numel(teststr)
freq = 4×1
0.3000 0.0500 0.4000 0.2500
% display as a bar chart
bar(freq)
xticklabels(num2cell(categories))

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!