How to define answer value which i got it from ''for'' loop as a variable and use it in the same loop?

1 view (last 30 days)
In the following assignment the last answer is "h2toplam = total + 11698.5"
for sun = 1:1:p ;
if load(sun,1) < pv (sun,1);
h = pv(sun,1)-load (sun,1);
n(sun,1) = double (h); % (PV'den artan Enerji)
deger = sum (n); % (Artan Enerjinin toplam değeri)
total = deger;
h2toplam = total + 11698.56;
i want to use the last answer as a varaible in the same loop in the following assignment. When i use h2toplam instead of h2 in while loop it gives error "Undefined function or variable 'h2toplam' ". Please help me how to define h2toplam as a varaibel.
elseif load(sun,1) > pv (sun,1);
y = load(sun,1) - pv (sun,1); % (Eksik olan Enerjinin De?eri)
v = pemfc(find( pemfc > y(:,1) , 1 ) ); % very important (Eksik olan enerjinin PEMFC'daki belirli aralikta çal??mas?)
x = -v ; % (Eksik olan enerjinin - olarak gösterilmesi)
j(sun,1) = double (x); % (Eksik olan enerjinin PEMFC'daki belirli aralikta çalismasi)
k = 1;
while h2 > 0;
for q = 1:1:e;
h2miktar = (depo(q,1)*(243.71/731));
m(q,1) = double (h2miktar); % (Hidrojen miktari)
h2t = sum (m); % Toplam Hidrojen Miktari
g = h2 + (depo(q,1)*(243.71/731));
h2 = g;
h2v(k) = h2; % (Toplam Hidrojen miktari kac saat icin yeterli oldugunu gosterir)
k = k + 1;
  1 Comment
Image Analyst
Image Analyst on 10 Jul 2019
That code should be OK. You do not need to allocate a variable in advance -- you can create it automatically the first time you use it, as you did. There must be something else wrong with your code. Please post entire code plus entire error message (all the red text).

Sign in to comment.

Accepted Answer

vidhathri bhat
vidhathri bhat on 10 Jul 2019
Hi,
When you run the loop, on the first iteration if it is going to the else part, then there is no h2toplam variable defined anywhere. Make sure that it is going to the if case for the first iteration or initialize the h2toplam variable with appropriate value before entering the loop.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!