how to debug Index exceeds matrix dimensions
Show older comments
figure;
load('usborder.mat','x','y','xx','yy');
rng(3,'twister')
nStops = 200;
stopsLon = zeros(nStops,1);
stopsLat = stopsLon;
n = 1;
while (n <= nStops)
xp = rand*1.5;
yp = rand;
if inpolygon(xp,yp,x,y)
stopsLon(n) = xp;
stopsLat(n) = yp;
n = n+1;
end
end
plot(x,y,'Color','red');
hold on
plot(stopsLon,stopsLat,'*b')
hold off
idxs = nchoosek(1:nStops,2);
dist = hypot(stopsLat(idxs(:,1)) - stopsLat(idxs(:,2)), ...
stopsLon(idxs(:,1)) - stopsLon(idxs(:,2)));
lendist = length(dist);
tsp = optimproblem;
trips = optimvar('trips',lendist,1,'Type','integer','LowerBound',0,'UpperBound',1);
ERROR:-
Index exceeds matrix dimensions.
Error in sym/subsref (line 841)
R_tilde =
builtin('subsref',L_tilde,Idx);
3 Comments
Walter Roberson
on 28 Mar 2018
Please show the complete error message -- everything in red.
The error message is occurring in one of the routines in the symbolic toolbox, but it is not obvious that you have any symbolic variables at all.
MAYANK KUMAR
on 29 Mar 2018
MAYANK KUMAR
on 29 Mar 2018
Answers (1)
the cyclist
on 28 Mar 2018
It's difficult to answer that without knowing how much you know about debugging MATLAB in general. I would recommend looking at this documentation page about debugging.
Then, specifically, a very powerful technique is to type
dbstop if error
into the command window, and execute your code. That command will make MATLAB to stop execution at the line of the code that throws the error, and put you into the editor window at the offending line. Then, you can see the values of all the variables, their size, etc.
After you are done debugging, you will then want to type
dbclear if error
to go back to normal operation.
9 Comments
MAYANK KUMAR
on 29 Mar 2018
Is that the whole stack displayed by the error message? I would expect more. In particular, I would expect to see at the bottom of the stack which line of your code is ending up calling syms/subsref.
In any case, the error message is clear, Idx exceeds the number of elements in L_tilde.
Since you're now stopped in the debugger, what does
dbstack
show?
Also, clearly you're using some symbolic variable here, yet the code you show is not designed for symbolic variables. Are you sure that your load does not load symbolic variables?
MAYANK KUMAR
on 29 Mar 2018
MAYANK KUMAR
on 29 Mar 2018
Walter Roberson
on 29 Mar 2018
Look above the code for syms, to the upper right corner. Do you see the small downward pointing triangle in a circle? Right click on that and choose the "Undock editor" menu item. When that is done, you will see that the error message has a number of more lines than what you have posted: we need all of the lines in red.
MAYANK KUMAR
on 29 Mar 2018
Guillaume
on 29 Mar 2018
When you're stopped in the debugger because of the error (i.e. you've issued dbstop if error, ran the code, and you now have a K>> prompt (not plain >>), what does
dbstack
show?
MAYANK KUMAR
on 29 Mar 2018
Guillaume
on 29 Mar 2018
Yes, then type
dbstack
and show the result
Categories
Find more on Code Performance 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!


