Error: In an assignment A(I) = B, the number of elements in B and I must be the same.

1 view (last 30 days)
I'm trying to simulate a battery connected to a PV system. However, there seems to be an issue with column vector since I get this error message: In an assignment A(I) = B, the number of elements in B and I must be the same at line 30 (BatteriLaddning(i)=diff(i)). I guess the issue is not limited to that line, but the first value ends up there and therefore the error occurs there. Here's my code:
Data=importdata('BatteriData.txt');
Produktion=zeros(8760,1);
Konsumtion=zeros(8760,0);
Diff=zeros(8760,0);
BatteriLaddning=zeros(8760,1);
Overskott=zeros(8760,1);
Produktion=Data(:,1); %ta ut första kolumnen
Konsumtion=Data(:,2); %ta ut andra kolumnen
Diff=Data(:,3); %ta ut tredje kolumnen
AntalBatterier=2;
BatteriKapacitet=7*AntalBatterier;
EffektKapacitet=2*AntalBatterier;
OverskottH=0;
%Läs in de tre första från extern fil
for i=1:8760
if (diff(i)>0&BatteriLaddning(i)>=BatteriKapacitet)
Overskott(i)=diff(i);
OverskottH=OverskottH+1;
elseif (diff(i)<0&diff(i)>EffektKapacitet)
Overskott(i+1)=diff(i)-EffektKapacitet;
OverskottH=OverskottH+1;
if (i==1)
BatteriLaddning(i)=EffektKapacitet;
else
BatteriLaddning(i)=BatteriLaddning(i-1)+EffektKapacitet;
end
else
if(i==1)
BatteriLaddning(i)=diff(i);
else
BatteriLaddning(i)=BatteriLaddning(i-1)+diff(i);
end
end
end

Accepted Answer

Thorsten
Thorsten on 29 Sep 2015
Edited: Thorsten on 29 Sep 2015
I think it shouldn't be diff(i) but
Diff(i)
There are some further issues with your code; You define
BatteriLaddning=zeros(8760,1);
AntalBatterier=2;
BatteriKapacitet=7*AntalBatterier;
EffektKapacitet=2*AntalBatterier;
so BatteriKapacitet=14 and EffektKapacitet = 4. Consider the first if clause
if (diff(i)>0&BatteriLaddning(i)>=BatteriKapacitet)
This would always be false, because BatteriLaddning(i) is 0 and cannot be >= 14.
Similar for the second clause
elseif (diff(i)<0&diff(i)>EffektKapacitet)
This will never be entered, because when diff(i) is smaller than zero, it cannot be greater than EffektKapacitet=4.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing 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!