Only first for loop is being executed. What im i doing wrong?

The code is part of a file for calculating reverb time of a room under set conditions.
MF = input('Enter the measurement frequency required in Hz(Default =125Hz): ');
if isempty(MF)
MF = 125; %default value for MF if this is left blank, using an if statement
end
%adding the absorption coefficients of the materials in use
%into the code for each of the 6 frequencies
for MF = 125; %frequency at which the absorption coefficients below apply
a1 = 0.8; %absorption coefficient for carpet on foam
a2 = 0.013; %absorption coefficient for plaster on brick
a3 = 0.29; %absorption coefficient for plasterboard
a4 = 0.35; %absorption coefficient for glass
a5 = 0.28; %absorption coefficient for wood
end
for MF = 250; %frequency at which the absorption coefficients below apply
a1 = 0.24; %absorption coefficient for carpet on foam
a2 = 0.015; %absorption coefficient for plaster on brick
a3 = 0.10; %absorption coefficient for plaster on plasterboard
a4 = 0.25; %absorption coefficient for glass
a5 = 0.22; %absorption coefficient for wood
end
for MF = 500; %frequency at which the absorption coefficients below apply
a1 = 0.57; %absorption coefficient for carpet on foam
a2 = 0.02; %absorption coefficient for plaster on brick
a3 = 0.05; %absorption coefficient for plaster on plasterboard
a4 = 0.18; %absorption coefficient for glass
a5 = 0.17; %absorption coefficient for wood
end
for MF = 1000; %frequency at which the absorption coefficients below apply
a1 = 0.69; %absorption coefficient for carpet on foam
a2 = 0.03; %absorption coefficient for plaster on brick
a3 = 0.04; %absorption coefficient for plaster on plasterboard
a4 = 0.03; %absorption coefficient for glass
a5 = 0.09; %absorption coefficient for wood
end
for MF = 2000; %frequency at which the absorption coefficients below apply
a1 = 0.71; %absorption coefficient for carpet on foam
a2 = 0.04; %absorption coefficient for plaster on brick
a3 = 0.07; %absorption coefficient for plaster on plasterboard
a4 = 0.04; %absorption coefficient for glass
a5 = 0.10; %absorption coefficient for wood
end
for MF = 4000; %frequency at which the absorption coefficients below apply
a1 = 0.73; %absorption coefficient for carpet on foam
a2 = 0.05; %absorption coefficient for plaster on brick
a3 = 0.09; %absorption coefficient for plaster on plasterboard
a4 = 0.05; %absorption coefficient for glass
a5 = 0.11; %absorption coefficient for wood
end

 Accepted Answer

It is a bit unclear why you are attempting to use for-loops here. From the looks of things, it appears that maybe you need to use if-tests instead. E.g.,
if MF == 125; %frequency at which the absorption coefficients below apply
a1 = 0.8; %absorption coefficient for carpet on foam
a2 = 0.013; %absorption coefficient for plaster on brick
a3 = 0.29; %absorption coefficient for plasterboard
a4 = 0.35; %absorption coefficient for glass
a5 = 0.28; %absorption coefficient for wood
end
if MF == 250; %frequency at which the absorption coefficients below apply
a1 = 0.24; %absorption coefficient for carpet on foam
a2 = 0.015; %absorption coefficient for plaster on brick
a3 = 0.10; %absorption coefficient for plaster on plasterboard
a4 = 0.25; %absorption coefficient for glass
a5 = 0.22; %absorption coefficient for wood
end
if MF == 500; %frequency at which the absorption coefficients below apply
a1 = 0.57; %absorption coefficient for carpet on foam
a2 = 0.02; %absorption coefficient for plaster on brick
a3 = 0.05; %absorption coefficient for plaster on plasterboard
a4 = 0.18; %absorption coefficient for glass
a5 = 0.17; %absorption coefficient for wood
end
if MF == 1000; %frequency at which the absorption coefficients below apply
a1 = 0.69; %absorption coefficient for carpet on foam
a2 = 0.03; %absorption coefficient for plaster on brick
a3 = 0.04; %absorption coefficient for plaster on plasterboard
a4 = 0.03; %absorption coefficient for glass
a5 = 0.09; %absorption coefficient for wood
end
if MF == 2000; %frequency at which the absorption coefficients below apply
a1 = 0.71; %absorption coefficient for carpet on foam
a2 = 0.04; %absorption coefficient for plaster on brick
a3 = 0.07; %absorption coefficient for plaster on plasterboard
a4 = 0.04; %absorption coefficient for glass
a5 = 0.10; %absorption coefficient for wood
end
if MF == 4000; %frequency at which the absorption coefficients below apply
a1 = 0.73; %absorption coefficient for carpet on foam
a2 = 0.05; %absorption coefficient for plaster on brick
a3 = 0.09; %absorption coefficient for plaster on plasterboard
a4 = 0.05; %absorption coefficient for glass
a5 = 0.11; %absorption coefficient for wood
end
The logic could be made simpler with if-elseif-else-end logic, or a switch statement. This would easily allow you to have some sort of error catch if the user did not input an acceptable value for MF. E.g.,
if MF == 125; %frequency at which the absorption coefficients below apply
etc
elseif MF == 250; %frequency at which the absorption coefficients below apply
etc
:
elseif MF == 4000; %frequency at which the absorption coefficients below apply
etc
else
error('Invalid MF');
end

More Answers (1)

Thanks James, that fixed it. I was trying to use if-else if statements but this returned parse errors always and i got confused and decided to use for loops.
But now it works perfectly.
Thanks a lot
Ed

Community Treasure Hunt

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

Start Hunting!