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

조회 수: 11 (최근 30일)
OMAYMA KHOURIBECH
OMAYMA KHOURIBECH 2023년 3월 18일
편집: DGM 2023년 3월 18일
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?

답변 (2개)

Stephen23
Stephen23 2023년 3월 18일
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 2023년 3월 18일
편집: DGM 2023년 3월 18일
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))

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by