0
votes

I am new to matlab in this assignment I have to give In an assignment an average for a gradebook and the assign a letter grade to each.

This is the error message I receive: In an assignment A(:) = B, the number of elements in A and B must be the same.

Code:

display(gradebook);

A = table2array(gradebook);


loopend = size(A,1)


for i=1:loopend
    average(i) = mean(A(i,1:5));
end

for i=1:loopend

if (average(i) <= 59)
    letter(i) = 'E' ;
elseif (average(i) <= 69)
    letter(i) = 'D' ;
elseif (average(i) <= 79)
    letter(i) = 'C' ;
elseif (average(i) <= 85)
    **letter(i) = 'C+' ;**
elseif (average(i) <= 89)
    **letter(i) = 'B+' ;**
elseif (average(i) <= 100)
    letter(i) = 'A' ;
end
end

display(letter)

The problem arises in the fact that I want to have a C+ and B+, It only accepts 1 character within the ''. Is there a way to fix this?

2

2 Answers

3
votes

Strings can be stored in cell array - initialized as {}. Note that you have to wrap every item of this array into cell.

letter = {};
letter(1) = {'A+'};
letter(2) = {'B'};
letter(3) = {'B+'};

display(letter);
display(letter(1));

Apparently string arrays are supported in newer versions of MATLAB (starting with R2016b).

1
votes

Yes, you cannot squeeze 2 values in there. So, either your spots have to be something that can hold multiple values (for example cell or string array as in @barbsan answer), or you need to give 2 spots in the char array to the entry:

letter(i, :) = 'C ';
...
letter(i, :) = 'C+';