- 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
6 次查看(过去 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!