How to show the bin' percentage values' in a histogram?

26 ビュー (過去 30 日間)
Maria Amr
Maria Amr 2021 年 3 月 4 日
回答済み: Maria Amr 2021 年 3 月 4 日
Mukul Rao, I am really new in MATLAB and I would appreciate if direct me about this rodimentary question. I want to show the bin counts "percentage values" in histogram and my code is below but something is wrong and I can not find it!! Thank you n advance!
clc;
clear all;
close all;
%.................................................
data=xlsread('A');
dens=data(:,1);%gr/cm3
x=dens(1:1:end);
hist(x,24);
binranges = -0.06:0.08;
[bincounts] = histc(x,binranges);
percentagevalues = bincounts./sum(bincounts) * 100;
figure(2)
bar(binranges,bincounts,'histc')
for i = 1:length(bincounts)-1
x = (binranges(i) + binranges(i+1))/2;
y = bincounts(i)+1;
text(x,y,num2str(bincounts(i),'%3.2f'),'HorizontalAlignment','center');
end
  3 件のコメント
Maria Amr
Maria Amr 2021 年 3 月 4 日
編集済み: Maria Amr 2021 年 3 月 4 日
Cris LaPierre, Thank you!I am working with an old version 2013a but I hav run it in a 2020a as well and it does not show the figure 2 as well. The figure(2) should show final results.
Maria Amr
Maria Amr 2021 年 3 月 4 日
Cris LaPierre It is not working in any version! and I can not fine the issue!

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 3 月 4 日
The issue is with how you defined your binranges. When you don't specify a step size, the colon operator uses 1.
binranges = -0.06:0.08
binranges = -0.0600
Since bin edges must contain at least 2 values, the result of histc is bincounts=0. Run your code with your desired stepsize and it will work.
binranges = -0.06:0.01:0.08
binranges = 1×15
-0.0600 -0.0500 -0.0400 -0.0300 -0.0200 -0.0100 0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800

その他の回答 (1 件)

Maria Amr
Maria Amr 2021 年 3 月 4 日
Cris LaPierre Really appreciated!

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by