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 日

0 投票

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 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by