how to plot histogram from table?

12 views (last 30 days)
Hi i am new for Matlab, i got a table and want histogram to show the distribution of student grade. How can i plot it?
format longG;
data=xlsread('data.xls');
StudentID=data(:,1);
%double(ID);
no_of_std=length(StudentID);
Coursework=data(:,2);
Exam=data(:,3);
FinalMarks=0.4*Coursework + 0.6*Exam;
a= input('Enter the scale boundary for A: ');
b= input('Enter the scale boundary for B: ');
c= input('Enter the scale boundary for C: ');
d= input('Enter the scale boundary for D: ');
N=length(FinalMarks);
FinalGrade = cell(N,1);
for i = 1:N
if FinalMarks(i) >= a
FinalGrade{i} = 'A';
elseif FinalMarks(i) >= b % final(i)<A has been exclude before already
FinalGrade{i} = 'B';
elseif FinalMarks(i) >= c
FinalGrade{i} = 'C';
elseif FinalMarks(i) >= d
FinalGrade{i} = 'D';
else
FinalGrade{i} = 'F';
end % Close the IF branches
end % Close the FOR loop
count_A = sum(strcmp(FinalGrade,'A')); % this part to count no. of A,B,C,D,F.
count_B = sum(strcmp(FinalGrade,'B'));
count_C = sum(strcmp(FinalGrade,'C'));
count_D = sum(strcmp(FinalGrade,'D'));
count_F = sum(strcmp(FinalGrade,'F'));
t1=table(StudentID, Coursework, Exam, FinalMarks, FinalGrade);
result= sortrows(t1, 'FinalMarks', 'descend');
display(result);
The histogram i want is x-axis(FinalGrade) become: A B C D F and y-axis become corresponding no. of students.
  1 Comment
Akira Agata
Akira Agata on 19 Sep 2017
Seems that bar will generate what you want to obtain, like "bar([count_A, count_B, count_C, count_D, count_F)"

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 21 Sep 2017
This doesn't really have anything to do with tables, you just want a histogram on discrete values.
Use a categorical variable:
>> FinalGrade = categorical(randi(5,100,1),1:5,{'A' 'B' 'C' 'D' 'F'});
>> histogram(FinalGrade)
If you version of MATLAB doesn't support histogram on categorical, use hist instead.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!