Grading system in excel sheet based on marks

I am tyring to paste grade on excel sheet based on marks
i do this but this shows only F in excel sheet .. i want if anybody has marks >=80 then want to show A grade .. and if anybody hay >=70 then B
and if anybody hay less than 70 then want to show F
clc;clear all
filename = 'PROJECT..xlsx';
table = readtable(filename);
data = xlsread(filename);
[r,c] = size(data);
total_courses = c;
table.SumofMarks = zeros(r,1);
table.Percentage = zeros(r,1);
table.StudentGrade = repmat({''},r,1);
% Sum of marks in different courses and their percentage.
format bank
Sum_of_Marks = sum(data,2);
Student_Percentage = Sum_of_Marks./c;
T = horzcat(Sum_of_Marks,Student_Percentage);
N = length(Student_Percentage);
R = reshape(Student_Percentage,1,[]); % convert matrix to row vector
for i = 1:1:N
if R>=80
table.StudentGrade{i} = 'A';
elseif R>=70
table.StudentGrade{i} = 'B';
elseif R <70
table.StudentGrade{i}= 'F';
end
end
writetable(table,filename)
xlswrite(filename,T,1,'H2')
i tried this but this code doesnt work
how i do this
i attached excel file
please help

댓글 수: 3

You forgot to attach 'PROJECT..xlsx'.
busy girl
busy girl 2021년 1월 7일
@Image Analyst.. i update question and attach PROJECT.XLsx file.. please check
busy girl
busy girl 2021년 1월 8일
any help plzzzzzzz

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

답변 (1개)

Alan Stevens
Alan Stevens 2021년 1월 8일

0 개 추천

Shouldn't you have
for i = 1:1:N
if R(i)>=80
table.StudentGrade{i} = 'A';
elseif R(i)>=70
table.StudentGrade{i} = 'B';
elseif R(i) <70
table.StudentGrade{i}= 'F';
end
end

댓글 수: 11

i tried your code and this shows an error ..
Undefined function or variable 'N'.
Error in lab4 (line 18)
for i = 1:1:N
You have N defined two lines above this!! My code simply suggests replacing R with R(i). I can't test your code as you haven't supplied the xlsx file.
busy girl
busy girl 2021년 1월 9일
i aready attached xlsx file.. did you check my complete question.. i already attached excel file for refrence
The following runs for me without any errors:
filename = 'PROJECT..xlsx';
table = readtable(filename);
data = xlsread(filename);
[r,c] = size(data);
total_courses = c;
table.SumofMarks = zeros(r,1);
table.Percentage = zeros(r,1);
table.StudentGrade = repmat({''},r,1);
% Sum of marks in different courses and their percentage.
format bank
Sum_of_Marks = sum(data,2);
Student_Percentage = Sum_of_Marks./c;
T = horzcat(Sum_of_Marks,Student_Percentage);
N = length(Student_Percentage);
R = reshape(Student_Percentage,1,[]); % convert matrix to row vector
for i = 1:N
if R(i)>=80
table.StudentGrade{i} = 'A';
elseif R(i)>=70
table.StudentGrade{i} = 'B';
elseif R(i) <70
table.StudentGrade{i}= 'F';
end
end
writetable(table,filename)
xlswrite(filename,T,1,'H2')
Hmm. Although my previous listing runs without error, the results are nonsense. For some reason the data is read in incorrectly. I had to coopy some of the original spreadsheet and paste it into another one to get sensible results. Here is a working code with sensible results:
filename = 'PROJECTb.xlsx';
fname = 'PROJECTRESULT.xlsx';
table = readtable(filename);
data = xlsread(filename);
[r,c] = size(data);
total_courses = c;
table.SumofMarks = zeros(r,1);
table.Percentage = zeros(r,1);
table.StudentGrade = repmat({''},r,1);
% Sum of marks in different courses and their percentage.
format bank
Sum_of_Marks = sum(data,2);
Student_Percentage = Sum_of_Marks./total_courses;
T = horzcat(Sum_of_Marks,Student_Percentage);
N = length(Student_Percentage);
table.SumofMarks = Sum_of_Marks;
table.Percentage = Student_Percentage;
R = Student_Percentage;
for i = 1:N
if R(i)>=80
table.StudentGrade{i} = 'A';
elseif R(i)>=70
table.StudentGrade{i} = 'B';
elseif R(i) <70
table.StudentGrade{i}= 'F';
end
end
writetable(table,fname)
The PROJECTb.xlsx file is attached.
busy girl
busy girl 2021년 1월 9일
Hi.. i tried your code.. and this code shows if total marks is less than 40 .. where as I want to show if marks is less than 40 in any subject .. I want if any student get less than 40 marks in any subject then want to show F grade..
@Alan Stevens
busy girl
busy girl 2021년 1월 9일
any help please @Alan Stevens
Alan Stevens
Alan Stevens 2021년 1월 9일
편집: Alan Stevens 2021년 1월 9일
I hadn't noticed the number 40 in your coding anywhere. My previous code sets F if total marks are less than 70. The code below shows one way of setting F if any mark is less than 40, or if the total is less than 70. This should be enough for you to modify if you want something else.
filename = 'PROJECTb.xlsx';
fname = 'PROJECTRESULT.xlsx';
table = readtable(filename);
data = xlsread(filename);
[r,c] = size(data);
total_courses = c;
table.SumofMarks = zeros(r,1);
table.Percentage = zeros(r,1);
table.StudentGrade = repmat({''},r,1);
I = find(data<40); %%%%%%%%%%%%%%%%%
[rowfail, colfail] = ind2sub(size(data),I); %%%%%%%%%%%%%%%%%
fails = unique(rowfail); %%%%%%%%%%%%%%%%%
% Sum of marks in different courses and their percentage.
format bank
Sum_of_Marks = sum(data,2);
Student_Percentage = Sum_of_Marks./total_courses;
T = horzcat(Sum_of_Marks,Student_Percentage);
N = length(Student_Percentage);
table.SumofMarks = Sum_of_Marks;
table.Percentage = Student_Percentage;
R = Student_Percentage;
for i = 1:N
if R(i)>=80
table.StudentGrade{i} = 'A';
elseif R(i)>=70
table.StudentGrade{i} = 'B';
elseif R(i) <70 || i==fails %%%%%%%%%%%%%%%%%
table.StudentGrade{i}= 'F';
end
end
writetable(table,fname)
hI ,, WHEN I try your code .. this shows an error
Operands to the || and && operators must be
convertible to logical scalar values.
Error in LABV1 (line 27)
elseif R(i) <41 && i==fails
Come to think of it, that line should probably be
elseif R(i) <70 || ismember(i,fails)
hi.. i tried your code and add conditions like these just replace numbers 70 to 40.. but this paste according to total marks where as i want if there is less than 40 marks in any subject then want to paste F grade ..
filename = 'PROJECT..xlsx';
table = readtable(filename);
data = xlsread(filename);
[r,c] = size(data);
total_courses = c;
table.SumofMarks = zeros(r,1);
table.Percentage = zeros(r,1);
table.StudentGrade = repmat({''},r,1);
I = find(data<41);
[rowfail, colfail] = ind2sub(size(data),I);
fails = unique(rowfail);
% Sum of marks in different courses and their percentage.
format bank
Sum_of_Marks = sum(data,2);
Student_Percentage = Sum_of_Marks./total_courses;
T = horzcat(Sum_of_Marks,Student_Percentage);
N = length(Student_Percentage);
table.SumofMarks = Sum_of_Marks;
table.Percentage = Student_Percentage;
R = Student_Percentage;
for i = 1:N
if R(i)>=41 && R(i)<=99
table.StudentGrade{i} = 'A';
elseif R(i) >0 && R(i)<=40 || ismember(i,fails)
table.StudentGrade{i}= 'F';
end
end
writetable(table,filename)

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

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2021년 1월 7일

댓글:

2021년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by