MATLAB does not recognize the matrix

I'm trying to create a GPA calculator for an assignment and I' m making use of matrix ii = 1:n, but MATLAB never makes use of this matrix and instead gives the message that the right and left sides are not compatible.
clc;clear
n = input('Number of courses taken this semester: ');
for kk = 1:n
u(kk) = input('Enter credit hour: ');
letter(kk) = input('Enter letter grade: ', 's');
if letter(kk) == 'A'
score(kk) = '4.00';
elseif letter(kk) == 'A-'
score = '3.67';
elseif letter(kk) == 'B+'
score(kk) = '3.33';
elseif letter(kk) == 'B'
score(kk) = '3.00';
elseif letter(kk) == 'B-'
score(kk) = '2.67';
elseif letter(kk) == 'C+'
score(kk) = '2.33';
elseif letter(kk) == 'C'
score(kk) = '2.00';
elseif letter(kk) == 'D'
score(kk) = '1.00';
elseif letter(kk) == 'F'
score(kk) = '0.00';
end
qlt_pts = u(kk) * score(kk);
end
fprintf('quality points: %f\n',qlt_pts);

Answers (2)

qlt_pts = u(kk) * score(kk);
your score is a char array. so what do you want this multiplication to give you?

5 Comments

Thank you, I realized that after I read your post. However, the program will not recognize the letter grades such as A- and C+ and it prints out the same message as before only for when I use A- and C+ (basically any letter that makes use of minuses and pluses. The rest works just as expected. Here's the updated code:
clc;clear
k = input('Number of courses taken this semester: ');
for kk = 1:(k-0)
credit_hr(kk) = input('Enter credit hour: ');
% disp(credit_hr(kk));
letter(kk) = input('Enter letter grade: ','s');
% disp(letter(kk));
if letter(kk) == 'A'
score(kk) = str2num('4.00');
elseif letter(kk) == 'A-'
score(kk) = str2num('3.67');
elseif letter(kk) == 'B+'
score(kk) = str2num('3.33');
elseif letter(kk) == 'B'
score(kk) = str2num('3.00');
elseif letter(kk) == 'B-'
score(kk) = str2num('2.67');
elseif letter(kk) == 'C+'
score(kk) = str2num('2.33');
elseif letter(kk) == 'C'
score(kk) = str2num('2.00');
elseif letter(kk) == 'D'
score(kk) = str2num('1.00');
elseif letter(kk) == 'F'
score(kk) = str2num('0.00');
end
qlt_pts(kk) = score(kk) * credit_hr(kk);
x = sum(qlt_pts);
y = sum(credit_hr);
end
fprintf('Sum of credit hours is: %f\n',y);
fprintf('quality point: %f\n',qlt_pts);
fprintf('Sum of qulaity points: %f\n',x);
GPA = x / y;
fprintf('GPA is: %3.2f\n',GPA);
Peng Li
Peng Li on 11 May 2020
Edited: Peng Li on 11 May 2020
As mentioned by Rik, use strcmp to determine if two char arrays are identical. Alternatively, using string. E.g., if string(letter(kk)) == "A", ...
You don’t need to use str2num to convert say ‘2.33’ back to double, you use 2.33 directly. When necessary better use str2double.
I would prefer to use a switch case block.
I tried using both the string function and the case block to see if it would work, but so far neither have worked. My trouble right now is letter grades with signs (B+, B-, etc.). The program displays an error message such as this:
Number of courses taken this semester: 1
Enter credit hour: 4
Enter letter grade: A-
Unable to perform assignment because the indices on the left side are
not compatible with the size of the right side.
Error in GPA_Calculator2 (line 6)
letter(kk) = input('Enter letter grade: ','s');
>>
letter(kk) = string(input('Enter letter grade: ','s'));
THANK YOU WALTER! THIS IS PRECISELY WHAT I WAS LOOKING FOR!!!!
THAT IS EXACTLY WHAT I WAS LOOKING FOR!!!!

Sign in to comment.

As Peng Li alludes to: your scores are char arrays. Why? It makes much more sense to store them are numeric arrays. That way you can also use an assignment like this:
score(kk)=2.33;
I would suggest you add an else with error handling. That way you can catch the user providing an incorrect letter grade. You also need to think if you want to keep all the letters that were entered. If you do, you are probably better off using a cell array.
A last point: if you want to compare char arrays you should be using the strcmp function.

Products

Release

R2019b

Asked:

on 9 May 2020

Edited:

on 11 May 2020

Community Treasure Hunt

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

Start Hunting!