# if statement in for loop with cell arrays

13 views (last 30 days)
nazlihan okcuer on 29 Oct 2019
Edited: Guillaume on 30 Oct 2019
I have this problem with matlab, I have to assign letter grades to the scores I have, but I couldn't do it. Can anybody help?
s.name={'Seda','Ayça','Deniz','Kerem','Mete'}
s.score={'90','45','68','86','82'}
N=lenght(s.score)
for i = 1:1:N
if (i)<100 & (i)>=90
elseif (i)<90 & (i)>=80
elseif (i)<80 & (i)>=70
elseif (i)<70 & (i)>=60
else
end
end
Guillaume on 30 Oct 2019
Edited: Guillaume on 30 Oct 2019
Can you have char varaible as values in discretize?
Yes,
>> score = [90, 45, 68, 86, 82];
>> discretize(score, [0 60 70 80 90 100], {'F', 'D', 'C', 'B', 'A'})
ans =
1×5 cell array
{'A'} {'F'} {'D'} {'B'} {'B'}
All discretize does is index the values array with the indices it would normally return. You can have pretty much anything that can be indexed as values.

Fabio Freschi on 29 Oct 2019
To stick with the OP snippet
1. I added semicolons all over the place
2. I changed the variable type in s.score to doubles
3. I preallocated the grades variable as string array
4. I changed the check on (i) with s.score{i}
5. I added <= 100 to include also the possibility of 100 score
6. I changed the single & to double && for logical scalar values
s.name={'Seda','AyÃ§a','Deniz','Kerem','Mete'};
s.score={90,45,68,86,82};
N=length(s.score);
for i = 1:1:N
if s.score{i}<=100 && s.score{i}>=90
elseif s.score{i}<90 && s.score{i}>=80
elseif s.score{i}<80 && s.score{i}>=70
elseif s.score{i}<70 && s.score{i}>=60
else
end
end

### Categories

Find more on Loops and Conditional Statements 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!

Translated by