Pie chart with a limited number of slices

4 views (last 30 days)
Henrik
Henrik on 24 Feb 2012
Answered: Charu on 29 Jan 2025
Hi!
I need to add a pie chart to the code below which only shows the ten most occurring letters in a text. Please help me to get this right.
fid=fopen('krypterad1.txt','rt');
text = fscanf(fid,'%c',Inf);
text = upper(text);
nummer = double(text);
sorterad = sort(text, 'descend')
pie(sorterad)
n = histc(nummer, 65:90);
for n =65:90;
for antal=1:n;
end
end
f = input('byt från ','s')
t = input('till ','s')
diff = double(t) - double(f)
for rulla = nummer+(diff)
if rulla<=64, rulla=rulla+26; end
if rulla>90, rulla=rulla-26; end
okodadtext = char(rulla);
fprintf('%s', okodadtext);
end
Thank you!
Best Regards/Henrik

Answers (1)

Charu
Charu on 29 Jan 2025
Hello Henrik,
According to my understanding you are trying to create a pie chart displaying the top 10 most frequent characters from your dataset, it's important to sort the characters based on their frequencies. In the code you provided, the histc function was used to calculate these frequencies. However, it is worth noting that histc is an older function and its use is now discouraged. For the R2024b version, it is recommended to use the histcounts function, which offers enhanced functionality and is more up-to-date.
Below is a code snippet demonstrating how to achieve this using histcounts:
% Calculate the histogram of letter frequencies (A-Z)
frequencies = histcounts(nummer, 64.5:1:90.5);
In this example, histcounts is employed to compute the frequency of each letter from 'A' to 'Z'. The function achieves this by specifying the ASCII range from 65 to 90, ensuring accurate frequency calculation.
For further details on the histcounts function, you can refer to the official documentation:
Additionally, for more information on why the use of histc is discouraged, you may consult the relevant documentation, you can refer to this part of the documentation.
Hope this is beneficial!

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!