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