How can adding a breakpoint cause an error?

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 个评论

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

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2013-1-25
编辑:Jan 2013-1-25
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 个评论

Thanks for the prompt response!
Neither eval() nor assignin() is in the code. What else works differently in debug mode and could cause errors? It seems to defeat the purpose of debugging that debug mode itself causes new errors... Anyway, here are the lines that seem pertinent:
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;
out2d.objc2(f,1:length(objc2D)) = objc2D;
if length(objc2) ~= length(objc2D)
disp(['Discrepancy: ' num2str(objc2) ' vs ' num2str(objc2D)])
end
...
end
...
end
Is that sufficient to determine the cause of the error?
Personally, it's now hard to see the correspondence between the pseudocode that you originally posted and the actual code that you've posted immediately above. I recommend you annotate the second version of the code as you did the first, saying where you set breakpoints, what steps you perform to produce the error and a copy/paste of the EXACT AND COMPLETE error messages.
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);
Trap this error with DBSTOP IF ERROR. Then evaluate
>> f, 1:length(objc2D)

请先登录,再进行评论。

Matt J
Matt J 2013-1-26
编辑:Matt J 2013-1-26
Is it possible that you've run, reached a breakpoint, then run again without first quitting out of DEBUG mode?

3 个评论

Similarly, is this a script or a function? If it's a script, then is it possible that you ran until reaching a breakpoint, quit out of debug mode, then ran again forgetting to clear all the variables from the base workspace of the changes induced by your previous run?
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.
No persistent variables either? What if you do "clear functions" first, and then set the breakpoints again and run?

请先登录,再进行评论。

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 个评论

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
Now do the same thing for out2d.objc2 (instead of objc2). You're saying that it's a 2D array, but is it really?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

提问:

2013-1-25

Community Treasure Hunt

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

Start Hunting!

Translated by