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

1 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2019-7-10
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).

请先登录,再进行评论。

采纳的回答

vidhathri bhat
vidhathri bhat 2019-7-10
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by