Multiple output values on an if else loop

I need to run the following code 10 times (as the number of Hact array) and for each Ht value take a WeightF value. Then plot these 10 values of WeightF. The problem is that it returns ten times the same value. Can anyone help??
%insert exeprimental data in xlsx file
data = xlsread('demo.xlsx');
data(1:9,:)
Hsunrise = 6.5;
Hsunset = 17.7;
for i=1:9
Ht = (12/(Hsunset - Hsunrise)).*(Hact-Hsunrise);
if Ht < -4
WeightF = 0.11;
elseif -4 < Ht < -3
WeightF = 0.11;
elseif -3 < Ht < -2
WeightF = 0.07;
elseif -2 < Ht < -1
WeightF = 0.08;
elseif -1 < Ht < 0
WeightF = 0.06;
elseif 0 < Ht < 1
WeightF = 0.05;
elseif 1 < Ht < 2
WeightF = 0.1;
elseif 2 < Ht < 3
WeightF = 0.51;
elseif 3 < Ht < 4
WeightF = 0.75;
elseif 4 < Ht < 5
WeightF = 0.95;
elseif 5 < Ht < 6
WeightF = 1;
elseif 6 < Ht < 7
WeightF = 0.9;
elseif 7 < Ht < 8
WeightF = 0.8;
elseif 8 < Ht < 9
WeightF = 0.59;
elseif 9 < Ht < 10
WeightF = 0.32;
elseif 10 < Ht < 11
WeightF = 0.22;
elseif 11 < Ht < 12
WeightF = 0.1;
elseif 12 < Ht < 13
WeightF = 0.08;
else
WeightF = 0.13;
end
disp(WeightF)
end

Réponses (2)

Stephan
Stephan le 10 Nov 2019
in every run of your loop you overwrite WeightF. Use indexing to avoid this:
WeightF(i) = ...
also using i for loop count is not recommended, because of the imaginary number operator i in Matlab - use ii instead.

3 commentaires

Stephan
Stephan le 10 Nov 2019
Modifié(e) : Stephan le 10 Nov 2019
what do you expect? You do the same calculation with the same values 9 times. It is obviously that you get the same result 9 times...
There is no connection from your data to the loop. I suspect that this is problem no. 2
ANTONIOS LIONIS
ANTONIOS LIONIS le 10 Nov 2019
I expect for each value of Ht, to take a value for WeightF accordingly.
Stephan
Stephan le 10 Nov 2019
Make sure to change the values of Hc inside the loop for every run accordingly to your data.

Connectez-vous pour commenter.

ANTONIOS LIONIS
ANTONIOS LIONIS le 10 Nov 2019

0 votes

Nope, only thing changed in now I have a 1X9 array output for WeightF with the same value!

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Commenté :

le 10 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by