how to plot histogram from table?

조회 수: 12 (최근 30일)
YYYYAAAAMMMM
YYYYAAAAMMMM 2017년 9월 18일
답변: Peter Perkins 2017년 9월 21일
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
Akira Agata
Akira Agata 2017년 9월 19일
Seems that bar will generate what you want to obtain, like "bar([count_A, count_B, count_C, count_D, count_F)"

댓글을 달려면 로그인하십시오.

채택된 답변

Peter Perkins
Peter Perkins 2017년 9월 21일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by