MATLAB Answers


How can adding a breakpoint cause an error?

Asked by 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;
     (other code)
     if questionableResults
      *test breakpoint*
   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

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

Log in to comment.

3 Answers

Answer by Jan Simon
on 25 Jan 2013
Edited by Jan Simon
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.


Show 1 older comment

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]
    if beta2 > alpha2
      objc2D = [objc2D obj]
  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;
        if questionableResults
    if questionableResults

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)

Log in to comment.

Answer by Matt J
on 26 Jan 2013
Edited by 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?


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?

Log in to comment.

Answer by Image Analyst
on 26 Jan 2013
Edited by 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.


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?

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!