how to debug Index exceeds matrix dimensions

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

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.
Index exceeds matrix dimensions.
Error in sym/subsref (line 841)
R_tilde =
builtin('subsref',L_tilde,Idx);
841 R_tilde = builtin('subsref',L_tilde,Idx);
after using dbstop if error, this is showing

Sign in to comment.

Answers (1)

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

R_tilde = builtin('subsref',L_tilde,Idx);
(line 841)
this is what its showing after using dbstop if error. i want to know how to debug this error.
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?
Index exceeds matrix dimensions.
Error in sym/subsref (line 841)
R_tilde =
builtin('subsref',L_tilde,Idx);
841 R_tilde = builtin('subsref',L_tilde,Idx);
this is the whole error its showing. I think the fault is in these two lines of code given below:-
tsp = optimproblem;
trips = optimvar('trips',lendist,1,'Type','integer','LowerBound',0,'UpperBound',1);
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.
this is all the red colored error
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?
after dbstop if error, sym.m file opens in editor window which show these many lines and point the green arrow at line 841
Yes, then type
dbstack
and show the result

Sign in to comment.

Asked:

on 28 Mar 2018

Commented:

on 29 Mar 2018

Community Treasure Hunt

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

Start Hunting!