Index exceeds matrix dimensions

조회 수: 3 (최근 30일)
Lisa
Lisa 2015년 9월 24일
댓글: Star Strider 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