Index exceeds matrix dimensions

6 次查看(过去 30 天)
Lisa
Lisa 2015-9-24
Hi, I'm trying to simulate a battery connected to a PV system. And it's been at least a years since I last worked with matlab so my knowledge is somewhat rusty. Anyway, when I use the following code I get an error that says Index exceeds matrix dimensions on line 9 (i.e. Konsumtion=Data(:,3)). The error moves between Konsumtion and Produktion. I tried fixing it by creating a column vector for the variables but that didn´t help. So here's the code:
Data=importdata('BatteriData.txt');
Produktion=zeros(8760,0);
Konsumtion=zeros(8760,0);
Diff=zeros(8760,0);
BatteriLaddning=zeros(8760,0);
Overskott=zeros(8760,0);
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=0:8760
if (diff(i)>0&&BatteriLaddning(i)>=BatteriKapacitet)
Overskott(i)=diff(i);
OverskottH=OverskottH+1;
elseif (diff(i)<0&&diff(i)>EffektKapacitet)
BatteriLaddning(i)=BatteriLaddning(i-1)+EffektKapacitet;
Overskott(i+1)=diff(i)-EffektKapacitet;
OverskottH=OverskottH+1;
else
Batteriladdning(i)=BatteriLaddning(i-1)+diff(i);
end
end

回答(3 个)

Stephen23
Stephen23 2015-9-24
编辑:Stephen23 2015-9-24
Look at the size of Data: it does not have as many columns as you think it does, so when you try to access the second or third column it throws an error. For example, if Data only has one column and you ask for the second column, then this will be an error.
Note that you can check the size of an array in several ways:
  • Use the size command.
  • Use the whos command.
  • Look at the variable in the workspace pane.
  • Open the workspace browser using workspace.
  • Double-click it to view that variable.
As you do not provide any test data we cannot run your code.

Lisa
Lisa 2015-9-24
Ok. So Data has the size 1x1. How do I increase it to 8760x3? I've tried to first create a matrix of zeros (Data=zeros(8760,3)) but Data is still only 1x1. I've attached the datafile to this comment so it can be possible for you to run the code.
  2 个评论
Stephen23
Stephen23 2015-9-24
编辑:Stephen23 2015-9-24
It works fine for me:
>> A = importdata('BatteriData.txt')
A =
0 2 2
0 2 2
0 2 2
0 2 2
0 2 2
0 2 2
...
>> size(A)
ans =
8760 3
What MATLAB version are you using? is it possible that there you have added any toolboxes or functions that have shadowed importdata ? Do you have multiple files with the same name, but saved in different locations? Perhaps you should check your MATLAB path, and see if there are any more "BatteriData.txt" files.
Lisa
Lisa 2015-9-24
The issue was that the data had , instead of . in the decimals, which is because I took it from a Swedish Excel Datasheet. I still have issues though, please refer to the answer I gave to the comment by Starstrider below.

请先登录,再进行评论。


Star Strider
Star Strider 2015-9-24
编辑:Star Strider 2015-9-24
You probably have read it in as a (1x1) cell array. To create a double array of size (8760x3), use the cell2mat function:
DataIn=importdata('BatteriData.txt');
Data = cell2mat(DataIn);
The cell2mat function was introduced before R2006a, so you should have it.
  2 个评论
Lisa
Lisa 2015-9-24
I found the issue, which was related to the fact that my Excel worksheet was in Swedish (i.e. , instead of . for decimals). However, new errors have occured. Now there is something wrong with BatterLagring(i)=diff(i) where it says that the number of elements in i and diff(i) must be the same. But according to me they are, since i is a double (1x1) and diff(i) is the number on place i in the column vector. Do you know what the issue might be? I've done a few changes in the code, the results can be seen below.
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
Star Strider
Star Strider 2015-9-24
Without seeing the file you are using, it is difficult to say.
However, MATLAB is case-sensitive (upper-case and lower-case letters are significant in variable names, function names, and such), so that the built-in function diff and your data vector ‘Diff’ are entirely different.
Notice that diff(i), since ‘i’ is a scalar and not a vector, will return an empty value. This will have unpredictable effects in your if block.
Also, although you have not done that in your code here, please do not ever name any of your own variables or functions to be the same as MATLAB built-in functions. This is called ‘overshadowing’ or ‘shadowing’, and can cause serious problems.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by