File Input and Grading script

6 views (last 30 days)
Don Sevcik
Don Sevcik on 21 Jan 2015
Commented: dpb on 22 Jan 2015
I'm new to Matlab. I'm trying to learn some basic functions. I want to import a file with grades. After sanity checks, I want to strip off the lowest score, then average it. Then plot this. Is this a best practice method below?
function grades = loadData(filename, checkErrors)
% function takes a grades-csv and TRUE/FALSE to check for errors.
% if check ERROR true, finds errors in data and prints.
% returns a grades matrix grades.
GRADE_SCALE = [12 10 7 4 0 2 -3];
input = importdata(filename, ',');
grades = input.data;
studentIDs = input.textdata(2:end,1);
nStudents = size(grades,1);
nAssigns = size(grades,2);
if (checkErrors==true)
%error checking - student IDs
[~, index] = unique(studentIDs);
notUniqueIDs = studentIDs;
notUniqueIDs(index) = [];
for i = 1:length(notUniqueIDs)
fprintf('Duplicate student ID %s.\n',notUniqueIDs{i});
end
%error checking - student grades
for i = 1:nStudents
for j = 1:nAssigns
if ~ismember(grades(i,j),GRADE_SCALE)
fprintf('Invalid Grade for Student %s, Assignment %d.\n', studentIDs{i}, j);
end
end
end
end
end
function gradesRounded = roundGrade(grades)
gradesRounded = [];
for i=1:length(grades)
if grades[i]<-1.5
gradesRounded[i] = -3;
if grades[i]>=-1.5 && grades[i]<1
gradesRounded[i] = 0;
if grades[i]>=1 && grades[i]<3
gradesRounded[i] = 2;
if grades[i]>=3 && grades[i]<5.5
gradesRounded[i] = 4;
if grades[i]>=5.5 && grades[i]<8.5
gradesRounded[i] = 7;
if grades[i]>=8.5 && grades[i]<11
gradesRounded[i] = 10;
if grades[i]>=11
gradesRounded[i] = 12;
end
end
% Take minimum value of each row in the matrix
function gradesFinal = computeFinalGrades(grades)
B = zeros( size(grades,1)-1, size(grades,2));
for i=1:size(grades,2)
x = grades(:,i);
maxIndex = find(x==min(x(:)),1,'first');
x(maxIndex) = [];
B(:,i) = x;
end
% Get average grade
average_grades = mean(B,2);
rows = Size(grades,1);
cols = Size(grades,2);
for i = 1:rows
for j=1:cols
end
end
end
function gradesPlot(grades)
bar(grades[0],grades[1]);
set(gca,'xticklabel',{'a','b','c','d','e','f','g','h','i','j','k','l'});
title('Student Rounded Grade Plots');
xlabel('Assignments');
ylabel('Grades');
legend('x = Assignments','y = Grades','Location','southwest');
end
% main program
% read input file
g = loadData(filename,true);
% get rounded grades
grades = roundGrade(g);
% final grades
grades2 = computeFinalGrades(grades);
% Plot Grades
gradesPlot(grades2);
  2 Comments
dpb
dpb on 22 Jan 2015
The routines I wrote...
>> type computeGrades
function finalgrade=computeGrades(grades)
if size(grades,1)==1 % if only one exam, grade is that
finalgrade=grades;
return
end
% at least two scores and more...
isThree=any(grades==-3,2); % who got a -3?
finalgrade(isThree)=-3; % no 2nd chance for them, apparently...
grades=sort(grades(~isThree,:),2); % those w/o a -3 to average
grades=grades(:,2:end); % and toss the lowest score
finalgrade(~isThree)=roundGrade(mean(grades,2)); % their ending avg
finalgrade=finalgrade(:); % return a column vector
>> type roundGrade
function avgGrade=roundGrade(grades)
values=[-3, 0, 2, 4, 7, 10, 12].'; % the allowable levels
avgGrade= interp1(values,values,grades,'nearest');
>>
It's unlikely these will directly help in an intro course (at least without a lot of 'splaining :) ) but I'll post them as a demo of "the Matlab way" to use the vector facility of Matlab to avoid looping and the succinctness possible via some of the supplied functionality. You just have to have had "time in grade" to think of things like interp1 for the rounding. There are any number of other ways, I chose this simply as a demo of the specific feature of 'nearest' lookup being "cute".
I pushed the other poster to try to think of the logical array and all, but it's an admitted stretch for a new initiate...

Sign in to comment.

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!