MATLAB Answers

Why is my nested if statement is not working.

1 view (last 30 days)
Lou
Lou on 1 Aug 2021
Commented: Lou on 1 Aug 2021
fid = fopen('studentData.txt', 'r');
while (~feof(fid))
str = fgetl(fid);
T = textscan(str,'%s %s %f %f %f %f %f %f %f %f');
disp(str);
Total = (T{1,3}+T{1,4}+T{1,5}+T{1,6}+T{1,7}+T{1,8}+T{1,9}+T{1,10})
Average = Total/10
mark1 = T{1,4};
mark2 = T{1,6};
mark3 = T{1,8};
mark4 = T{1,10};
if (mark1 >= 90)
disp('A+');
elseif (80 <= mark1 < 90)
disp('A');
if (70 <= mark1 < 80)
disp('A-');
elseif (60 <= mark1 < 70)
disp('B');
if (50 <= mark1 < 60)
disp('C');
elseif (40 <= mark1 < 50)
disp('D');
if (35 <= mark1 < 40)
disp('E');
else
disp('F');
end
end
end
end
end
fclose(fid)
  1 Comment
Rik
Rik on 1 Aug 2021
Your indentation seems to suggest you expect different behavior.
Have you tried setting a breakpoint at the start of your function and going through your code line by line?
Edit:
I just noticed you're doing this:
(80 <= mark1 < 90)
What do you expect to happen? There is an orange line under it in the editor. Don't ignore it.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 1 Aug 2021
In addition to Rik's comment above, you forgot to read the posting guidelines
which means you never learned that you were supposed to attach 'studentData.txt' to make it easy for us to run your code and help you. But anyway, try it this way:
fid = fopen('studentData.txt', 'rt');
lineCounter = 0;
while (~feof(fid))
str = fgetl(fid);
lineCounter = lineCounter + 1;
T = textscan(str,'%s %s %f %f %f %f %f %f %f %f');
fprintf('\nRead line #%d : "%s".\n', str);
Total = (T{1,3}+T{1,4}+T{1,5}+T{1,6}+T{1,7}+T{1,8}+T{1,9}+T{1,10})
Average = Total/10
mark1 = T{1,4};
mark2 = T{1,6};
mark3 = T{1,8};
mark4 = T{1,10};
if (mark1 >= 90)
fprintf('A score of %.1f is A+.\n', mark1);
elseif mark1 >= 80
fprintf('A score of %.1f is A.\n', mark1);
elseif mark1 >= 70
fprintf('A score of %.1f is A-.\n', mark1);
elseif mark1 >= 60
fprintf('A score of %.1f is B.\n', mark1);
elseif mark1 >= 50
fprintf('A score of %.1f is C.\n', mark1);
elseif mark1 >= 40
fprintf('A score of %.1f is D.\n', mark1);
elseif mark1 >= 35
fprintf('A score of %.1f is E.\n', mark1);
else
fprintf('A score of %.1f is F.\n', mark1);
end
end
fclose(fid)
Note that you don't need to check in a range between a high number and a low number because simply checking if it's above this high number means it will go in that one and skip all the rest. Think about it -- why check for the low number of the range??? You don't need to.

More Answers (2)

VBBV
VBBV on 1 Aug 2021
Edited: VBBV on 1 Aug 2021

Check the size of mark1 it may be cell array instead of scalar value that you want to do condition tests through nested If..else loops

  1 Comment
Rik
Rik on 1 Aug 2021
str='a b 1 2 3 4 5 6 7 8';
T = textscan(str,'%s %s %f %f %f %f %f %f %f %f');
mark1 = T{1,4}
mark1 = 2

Sign in to comment.


Walter Roberson
Walter Roberson on 1 Aug 2021
elseif (80 <= mark1 < 90)
As far as MATLAB is concerned, that means the same as
elseif all((80 <= mark1) < 90, 'all')
That is, 80 <= mark1 is calculated, returning 0 (false) or 1 (true), and that 0 or 1 is then to be compared to 90 .
It does not mean to mark1 should be tested to be between 80 (inclusive) and 90 (exclusive). MATLAB itself does not support that kind of distributed comparison (there is one specific Symbolic Toolbox function that does support those kind of comparisons)
You need to instead
elseif 80 <= mark1 && mark1 < 90
See though Image Analyst's post showing that you do not need to test the upper bounds if you code right.

Community Treasure Hunt

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

Start Hunting!