- 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.
Index exceeds matrix dimensions
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (3개)
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:
As you do not provide any test data we cannot run your code.
댓글 수: 0
Lisa
2015년 9월 24일
댓글 수: 2
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.
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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!