How can adding a breakpoint cause an error?

7 views (last 30 days)
Natalie
Natalie on 25 Jan 2013
I'm having problems debugging Matlab code since when I add a breakpoint, I get an error that doesn't happen without the breakpoint:
In my code I have an if statement to detect questionable results and display a message to the command line, but otherwise let the code run which it does with no issues. There were a few questionable results (after many iterations of expected results) that I wanted to investigate so I put a breakpoint within that if statement; however, once I did that, there is a repeatable error regarding the assignment on the line previous to the if statement (so it never gets to the actual breakpoint). The code runs fine without a breakpoint, but has an error with the breakpoint - what causes this?? And how can I avoid it??
I also tried adding a questionableResults = 1 flag and using that to move the breakpoint to later (as below) and putting a breakpoint in the 'other code' and also after the 'if C' statement, but eventually I always get the same error as with the original breakpoint. As soon as I remove the breakpoint, it runs to completion with no errors.
for i = 1:largeNumber
(code to find value1 and value2)
if C
A = value1;
B = value2; % the error is for this line if there is a breakpoint
if A~= B;
disp(['Warning: ' num2str(A) ' is not the same as ' num2str(B)]);
*original breakpoint*
questionableResults = 1;
end
(other code)
if questionableResults
*test breakpoint*
end
end %if C
*test breakpoint*
end %for i
The error is: 'Assignment has more non-singleton rhs dimensions than non-singleton subscripts' and occurs on the with the assignment of B.
Any insight would be appreciated. Thanks!
  1 Comment
Matt J
Matt J on 26 Jan 2013
It doesn't seem like you've shown your actual code. There are no subscripts in the line where you claim to see the error. A few preliminary recommendations
  • Show your actual code if you're not doing so
  • Paste your error messages in their entirety.
  • Use "dbstop if error" and see where the code stops and what the state of value2 etc... is at that point

Sign in to comment.

Answers (3)

Jan
Jan on 25 Jan 2013
Edited: Jan on 25 Jan 2013
The paraphrased pseudo code is less suitable for debugging in a forum. It is very likely that the problem is exactly in this parts of the code you have hidden.
Do you have any eval() or assignin() in your code? The dynamic creation of variables works differently in debug and non-debug mode.
  4 Comments
Natalie
Natalie on 28 Jan 2013
Edited: Natalie on 28 Jan 2013
My apologies for oversimplifying the original code; I should have realized that it was a more specific context issue. Here is the code with comments similar to the simplified code, denoting test breakpoints with * BP *:
for f = 1:numFrames
objc2 = [];
objc2D = [];
...
for obj = 1:numObj
if beta > alpha
objc2 = [objc2 obj]
end
if beta2 > alpha2
objc2D = [objc2D obj]
end
end
if ~isempty(objc2)
out2d.objc(f,1:length(objc2)) = objc2;
*newBP* out2d.objc2(f,1:length(objc2D)) = objc2D; % problem line
if length(objc2) ~= length(objc2D)
*BP* disp(['Discrepancy: ' num2str(objc2) ' vs ' num2str(objc2D)])
questionableResults = 1;
end
...
if questionableResults
*BP*
end
end
...
if questionableResults
*BP*
end
end
The error message that I get if there is a breakpoint at any of the * BP * above:
??? Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in ==> collision_cone2 at 203 out2d.objc2(f,1:length(objc2D)) = objc2D;
Error in ==> Main at 249 [cc_out]=collision_cone(data, testVar);
Matt J
Matt J on 28 Jan 2013
Trap this error with DBSTOP IF ERROR. Then evaluate
>> f, 1:length(objc2D)

Sign in to comment.


Matt J
Matt J on 26 Jan 2013
Edited: Matt J on 26 Jan 2013
Is it possible that you've run, reached a breakpoint, then run again without first quitting out of DEBUG mode?
  3 Comments
Natalie
Natalie on 26 Jan 2013
It's a function and throws an error before it gets to the breakpoint. It's not in debug mode when I call the master function that calls this function.
Matt J
Matt J on 26 Jan 2013
No persistent variables either? What if you do "clear functions" first, and then set the breakpoints again and run?

Sign in to comment.


Image Analyst
Image Analyst on 26 Jan 2013
Edited: Image Analyst on 26 Jan 2013
Set a breakpoint at this line:
B = value2; % the error is for this line if there is a breakpoint
before you execute it, say this in the command line:
K>> whos value2
K>> size(value2)
Tell us what it says.
  2 Comments
Natalie
Natalie on 28 Jan 2013
Here's the output of whos and size for the breakpoint on the line causing errors:
K>> whos objc2D
Name Size Bytes Class Attributes
objc2D 1x1 8 double
K>> size(objc2D)
ans =
1 1
Image Analyst
Image Analyst on 29 Jan 2013
Now do the same thing for out2d.objc2 (instead of objc2). You're saying that it's a 2D array, but is it really?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!