Index Out of Bounds

3 次查看(过去 30 天)
Brent
Brent 2011-5-24
I've inherited a program for vehicle dynamics analysis. The code is about 10 years old and I'm running into all kinds of errors. The code imports raw data into a post processing .m file. At the point where I'm getting an error is where it is defining clock wise and counter-clock wise inputs (ay). The variable "ay" is short for acceleration in the y direction (y direction is perpendicular to the forward motion of the vehicle and a function of time). I've posted the section where I getting this error so I hope it's not too vague. There is about 1600 lines of code that I'm trying to fix and make more robust for our raw data. It might be too much to post that much code; hopefully this works. Thanks for your help!
Here is the error:
??? Attempted to access mm2(3);
index out of bounds because
numel(mm2)=2.
Error in ==> OC_POST at 126
xlow_acc=a2(mm2(sl):mm2(sl+1));
Here is my code:
% Assign CW / CCW data from loops
dum=ones(size(ay));
a1=min(ay,limits*dum); % finds elements below positive maximum
a2=max(a1,-limits*dum); % finds elements above negative maximum
plot(a2)
axis([0 100 -0.2 0.2]);
axis 'auto x'
pause,
i1=1;n=length(a2);j1=1;
while 1
[dum,n1]=max(a2(j1:n));
mm1(i1)=j1-1+n1;i1=i1+1;j1=j1-1+n1;
if min(a2(j1:n))~=-limits, break, end;
[dum,n1]=min(a2(j1:n));
mm1(i1)=j1-1+n1;i1=i1+1;j1=j1-1+n1;
if max(a2(j1:n))~=limits, break, end;
end;
j1=n;
while 1
[dum,n1]=min(a2(j1:-1:1));
mm1(i1)=j1+1-n1;i1=i1+1;j1=j1+1-n1;
if max(a2(j1:-1:1))~=limits, break, end;
[dum,n1]=max(a2(j1:-1:1));
mm1(i1)=j1+1-n1;i1=i1+1;j1=j1+1-n1;
if min(a2(j1:-1:1))~=-limits, break, end;
end;
mm2=sort(mm1);
n=length(mm2);
if a2(mm2(1))==a2(mm2(2)), mm2=mm2(2:n); end;
n=length(mm2);
if a2(mm2(n))==a2(mm2(n-1)), mm2=mm2(1:n-1); end;
n1=ceil(length(mm2)/4);n2=floor(length(mm2)/4);
if a2(mm2(1))>a2(mm2(2)),nl=n1;nh=n2;sl=1;sh=3;
else nl=n2;nh=n1;sl=3;sh=1; end;
xlow_acc=a2(mm2(sl):mm2(sl+1));
ylow_str=swa(mm2(sl):mm2(sl+1));
for i1=2:nl, % original range 2:nl
xlow_acc=[xlow_acc a2(mm2(sl+(i1-1)*4):mm2(sl+1+(i1-1)*4))];
ylow_str=[ylow_str swa(mm2(sl+(i1-1)*4):mm2(sl+1+(i1-1)*4))];
end;
xhigh_acc=a2(mm2(sh):mm2(sh+1));
yhigh_str=swa(mm2(sh):mm2(sh+1));
for i1=2:nh, % original range 2:nh
xhigh_acc=[xhigh_acc a2(mm2(sh+(i1-1)*4):mm2(sh+1+(i1-1)*4))];
yhigh_str=[yhigh_str swa(mm2(sh+(i1-1)*4):mm2(sh+1+(i1-1)*4))];
end;
  3 个评论
Brent
Brent 2011-5-24
I know; the entire prgram is written like this.
Walter Roberson
Walter Roberson 2011-5-24
Search and replace!!

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2011-5-24
The code can end up with arbitrarily small mm1 or mm2, if the accelerations do not reach the limits.
It also appears to me that nearly all the a2 references are programmed incorrectly. There is no good reason to deliberately index them backwards (j1:-1:1), with the marginal exception that in min() or max() calls it makes a difference as to which copy of the minimum has its index returned, which would lead to mm1 and mm2 being constructed with the values in a different order. mm1 and mm2 are, however, sorted afterwards, so a difference in the order of construction will not make any difference in the result.
When I say that the references are "programmed incorrectly" I mean in the sense that it is incorrect programming practice to obscure the meaning of a section of code without good reason: it leads to errors and difficulty in debugging and greatly magnifies maintenance costs.
  1 个评论
Brent
Brent 2011-5-24
Thanks Walter! I'm overwhelmed with this code because most of the code is written much like what I posted. I'm not an expert with this type of coding (loop, if statements, etc.) so I'm having a hard time seeing what’s going with this section. The code is very sensitive to the data that is being imported. I've tested the code with the original .asc files from 10 years ago and they work fine, but when I try to create my data while following the same format, the program chokes. I keep retreating and checking my .asc file to check the formatting, but I'm not seeing any differences in the old test files. It's just not a robust code and it's difficult to decipher someone else's code (much respect to you guys for doing this everyday!)

请先登录,再进行评论。

更多回答(1 个)

bym
bym 2011-5-24
apparently the code does not account for length of mm2 being 2. Other than that I can't decipher it.
  2 个评论
Brent
Brent 2011-5-24
correct mm2 has a length of 2 and it's trying to index a number equal to sl; which is 3. I'm not sure why mm2 has a length of 2 when the "ay" matrix is 5003x1
Walter Roberson
Walter Roberson 2011-5-24
mm2 is created dynamically, starting with a single element. New elements are added each time that it is found that max(a2) *is* at the limit of acceleration -- because if it is *not* at the limit then you "break" out of the loop and stop dynamically extending mm2.
I presently have no idea why you would want to create one element of mm2 each time you hit the acceleration limit, and I presently have no idea why you would assume that there were at least 3 such places. It is not at all obvious to me what the code is intended to do.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by