how can i use input data in if..elseif?

12 views (last 30 days)
YYYYAAAAMMMM
YYYYAAAAMMMM on 17 Sep 2017
Edited: Jan on 17 Sep 2017
Hi all, i am matlab new leaner.
i got a practice question like this: import a excel file(score), transform the data to a table, and then user input the min. mark for different grades.(A-F) and sort in descending order. I dont understand how to use input value in the if..elseif. Here is my original code:
format longG;
data=xlsread('data.xls'); % read excel file
ID=data(:,1); % student ID
no_of_std=length(ID);
cw=data(:,2); %coursework mark
exam=data(:,3); % exam mark
final=0.4*cw + 0.6*exam; % final mark= 40%cw+60%exam
A= input('Enter the scale boundary for A: '); % user input for min. mark to get A
B= input('Enter the scale boundary for B: '); % user input for min. mark to get B
C= input('Enter the scale boundary for C: '); % user input for min. mark to get C
D= input('Enter the scale boundary for D: '); % user input for min. mark to get D
N=length(final);
for i=1:1:N
if i>= A
grade = 'A';
elseif i<A && i>=B
grade = 'B';
elseif i<B && i>=C
grade = 'C';
elseif i<C && i>=D
grade = 'D';
else grade = 'F';
t1=table(ID, cw, exam, final, grade); % output the 4 variables in table
result= sortrows(t1, 4, 'descend'); % sort column 'final' in descending order
display(result); % display the table
But i found that the loop part is not correct and 'grade' can not be shown in the table. How can i solve this problem thx.

Answers (1)

Jan
Jan on 17 Sep 2017
Edited: Jan on 17 Sep 2017
I assume you do not want to compare the loop index i but the data:
grade = cell(1, N);
for i = 1:N
if final(i) >= A % Not: i >= A
grade{i} = 'A'; % Do not overwrite grade in each iteration
elseif final(i) >= B % final(i)<A has been exclude before already
...
end % Close the IF branches
end % Close the FOR loop

Tags

Community Treasure Hunt

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

Start Hunting!