Index Out of Bounds
Show older comments
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 Comments
bym
on 24 May 2011
I have trouble distinguishing n1 from nl (gotta love it)
Brent
on 24 May 2011
Walter Roberson
on 24 May 2011
Search and replace!!
Accepted Answer
More Answers (1)
bym
on 24 May 2011
0 votes
apparently the code does not account for length of mm2 being 2. Other than that I can't decipher it.
2 Comments
Brent
on 24 May 2011
Walter Roberson
on 24 May 2011
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.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!