%histogram demo 1
%my histogram to specify the bins and count the frequencies
%use Prof. Zhang's 2007 Astronomy Class final grades as an example
clear;clc

%obtain the data from an ASCII data file
%column 1: student ID; column 2: student grade
%data starts from after the second column
%the delimiter is a space
data=dlmread('ASTR111_2007.dat','',2,0);

%obtain the grade from the second column
grad=data(:,2);

%specify the histogram bin
%the bin value indicates the centf the bin
bin=[5:10:95];

%count the frequenc
%initialize the frequency distribution
freq(10)=0;
for i=[1:132]
    %bin 1: grade from 0 to 10 
	if ((grad(i) >= 0) && (grad(i) < 10)), freq(1)=freq(1)+1; end
    %bin 2
	if ((grad(i) >= 10) && (grad(i) < 20)), freq(2)=freq(2)+1; end
	if ((grad(i) >= 20) && (grad(i) < 30)), freq(3)=freq(3)+1; end
	if ((grad(i) >= 30) && (grad(i) < 40)), freq(4)=freq(4)+1; end
	if ((grad(i) >= 40) && (grad(i) < 50)), freq(5)=freq(5)+1; end
	if ((grad(i) >= 50) && (grad(i) < 60)), freq(6)=freq(6)+1; end
	if ((grad(i) >= 60) && (grad(i) < 70)), freq(7)=freq(7)+1; end
	if ((grad(i) >= 70) && (grad(i) < 80)), freq(8)=freq(8)+1; end
	if ((grad(i) >= 80) && (grad(i) < 90)), freq(9)=freq(9)+1; end
 	if ((grad(i) >= 90) && (grad(i) <= 100)), freq(10)=freq(10)+1; end
end

freq

plot(bin,freq,'-*b')
xlabel('bin','Fontsize',14)
ylabel('Frequency','Fontsize',14)
title('My Frequency Counter','FontSize',18)
legend('Astro Grade','Location','northwest')
