MATLAB: Help the code won’t work as it ignores the switch blocks and puts in a different value of the string value.

homeworkswitch block

function [ F ] = MSP_Asgn4_Q2_25011721( n , Bank , P)
if Bank == 'A'
switch 'A'
case Bank == 'A'
3.5;
end
elseif Bank == 'B'
switch 'B'
case P <= 40000
3;
case (40000 < P) && (P<= 80000)
3.5;
case P >= 80000
4.21;
end
elseif Bank == 'C'
switch 'C'
case n <= 3
1;
case n > 3
4;
end
end
F = P * ((1 + (Bank / 100))^n);
end

Best Answer

Harry - when comparing strings, use the strcmp or strcmpi functions to do the comparison. When using equality (like you are doing in your code), you are more likely to not observe the correct response especially when one of the strings (being compared) has more characters than the other (or the number of characters are greater than one).
Please review the documentation for switch and compare that with how you are using it. For example,
switch 'A'
case Bank == 'A'
3.5;
end
First, your switch expression is a constant (the 'A' character) whereas this is (almost) always a variable. (Actually, I think it would always be a variable - what else would you put here?) The case expressions are then used to evaluate certain values for that variable. For integers, you could do a direct comparison; for strings, you would use strcmp. Here you are trying to evaluate Bank == 'A' which is already true given your if condition so it isn't clear why you would want to do this again. For your other cases, your code considers P or n and you try to do compare using an equality. This is not how case expressions are to be used. The documentation states this with A case_expression cannot include relational operators such as < or > for comparison against the switch_expression. To test for inequality, use if, elseif, else statements.
Third, for each of your case expressions (if they were to evaluate to true), you just have a single number. What variable should be assigned the (say) 3.5? How will this be used in your calculation for F (which for some reason includes the Bank variable which is a string??).
This seems like one of those homework problems where you are asked to solve the problem one way with if and elseif statements, and then do this again with switch and case. Is this the case?