MATLAB: If…elseif Statement Not Pulling Variables

if statement

Please I need help. I am trying to run the following arguments for three different cases using the if..elseif statement but it is popping up errors. I am trying to pull the values for the variables a, b and c for a subsequent calculation, depending on what statement is true from the three cases below.
if (E<0.01) and (F<L1) or (E>0.01) and (F<L2)
then
a = 0.98; b = 0.4846; c = 0.0868;
elseif (0.01<E<0.4) and (L3<F<L1) or (E>0.4) and (L3<F<L4)
then
a = 0.845; b = 0.5351; c = 0.0173;
elseif (E<0.4) and (F>L1) or (E>0.4) and (F>L4)
then
a = 1.065; b = 0.5824; c = 0.0609;
end
Thank you

Best Answer

That syntax is not MATLAB syntax at all: MATLAB if does not use a then statement, and while and and or do exist as functions, they cannot be used like that (only as functions, as the documentation clearly shows). It is also not possible to make two logical comparisons simultaneously, so 0.01<E<0.4 will always be an error.
You need to learn about basic MATLAB syntax and learn to read the documentation (read the links that I gave). These tutorials are a good place to start to learn MATLAB:
Here is a version that works without errors, as long as all of the input values are scalars:
if (E<0.01 && F<L1) || (E>0.01 && F<L2)
a = 0.98;
b = 0.4846;
c = 0.0868;
elseif (0.01<E && E<0.4 && L3<F && F<L1) || (E>0.4 && L3<F && F<L4)
a = 0.845;
b = 0.5351;
c = 0.0173;
elseif (E<0.4 && F>L1) || (E>0.4 && F>L4)
a = 1.065;
b = 0.5824;
c = 0.0609;
end
Note that if none of these conditions are met, then these variables will not be defined! (and ergo an else statement might be a good idea).
Related Question