MATLAB Answers

0

How to take all output from while loop?

Mohammad Sulaiman Stanekzai さんによって質問されました 2019 年 7 月 7 日
最新アクティビティ Star Strider
さんによって コメントされました 2019 年 7 月 8 日
I have while loop in following coding. it count data for a year. i am no able to take all one year data. It just shows last answer. I also tried double command it doesn't work too. Please help me how to take all one year data.
Thanks in advance.
clc;
clear;
clear all;
pv = xlsread('POWER PV');
p = size (pv,1);
load = xlsread('LOAD');
pemfc = xlsread('PEMFC 1');
l = size (pemfc,1);
i = 243.72; %(731 Wh)
h2 = 11698.56; %(48 Saat için h2 depolama)
for sun = 1:1:p ;
if load(sun,1) < pv (sun,1);
x = pv(sun,1)-load (sun,1);
elseif load(sun,1) > pv (sun,1);
y = load(sun,1) - pv (sun,1);
v = pemfc(find( pemfc > y(:,1) , 1 ) ); % very important .....
x = -v ;
while h2 > 0
g = h2 + (x(sun,1)*(243.71/731));
h2 = g;
h2;
end
else
x = 0;
end
n(sun,1) = double (x);
end

  0 件のコメント

サインイン to comment.

1 件の回答

Star Strider
回答者: Star Strider
2019 年 7 月 7 日
 採用された回答

I cannot run your code without your files.
However see if this solves the problem with your while loop:
k = 1;
while h2 > 0
g = h2 + (x(sun,1)*(243.71/731));
h2 = g;
h2v(k) = h2;
k = k + 1;
end
or, if ‘h2’ is not a scalar, save it as a cell array:
k = 1;
while h2 > 0
g = h2 + (x(sun,1)*(243.71/731));
h2 = g;
h2v{k} = h2;
k = k + 1;
end
Experiment to get the result you want.

  6 件のコメント

Star Strider
2019 年 7 月 8 日
Your code is a bit confusing.
This assignment:
g = h2 + (x(sun,1)*(243.71/731));
will use whatever the value of ‘sun’ is in that iteration. Revising slightly my original Answer, perhaps:
while h2 > 0
g = h2 + (x(sun,1)*(243.71/731));
h2 = g;
h2v(sun) = h2;
end
is what you want.
If you want ‘h2v’ to be a column vector, assign it as:
h2v(sun,:) = h2;
Dear sir, in the following assignment x is calculating 8784 values. By the way sun in index and equal 1:1:p, here p is values which it takes from excel file.
elseif load(sun,1) > pv (sun,1);
y = load(sun,1) - pv (sun,1);
v = pemfc(find( pemfc > y(:,1) , 1 ) ); % very important .....
x = -v ;
in the folowing assignment i want h2 for every x value which i found above but it just take the last value of x.
k = 1;
while h2 > 0
g = h2 + (x(sun,1)*(243.71/731));
h2 = g;
h2v(k) = h2;
k = k + 1;
i also tried your last coding but didn't work. Please help me
Star Strider
2019 年 7 月 8 日
If my previous code did not do what you want, I have no idea what the problem is. I have no other solutions. I do not have your data, or an example of what you want the result to be.
Guessing here.
Perhaps this is what you want:
elseif load(sun,1) > pv (sun,1);
y = load(sun,1) - pv (sun,1);
v = pemfc(find( pemfc > y(:,1) , 1 ) ); % very important .....
x = -v ;
if h2 > 0
g = h2 + (x(sun,1)*(243.71/731));
h2 = g;
h2v(sun) = h2;
end
else
It replaces your while loop with an if block.

サインイン to comment.



Translated by