2

I realise that this has been brought up before; but I am hoping that someone can clarify something for me. One of my variables is being calculated as a negative number at some point during my model run..however, this is a mistake (they can only be positive numbers). So I need to locate where this is happening a trace where the source of error is being produced.

The model is too large to do this manually, so I was hoping to use dbstop in some guise. I realise from reading the posts here that it is not simply a case of giving a condition (dbstop in test.m if var<0); so I was hoping to use a conditional if statement to display an error (disp 'error'), which I could use in: dbstop in test.m if error. However; what I am getting back is: 'Breakpoint expression 'error' resulted in an error. The error was Error using==>error Too many output arguments.' Can anyone suggest a way for me to isolate when my variable is becoming negative?

matlab_newby
  • 79
  • 1
  • 12
  • 1
    Can you post the actual code you are using to display the error, including the conditional if statement? It sounds to me like you are doing the right thing, but you may have a bug in that section of code. – Bill Cheatham Feb 11 '14 at 13:10
  • This is the sample code I was messing with to see if I could get it to stop:for i=(1:10); dbstop in 'db_test.m' if error; a=[20:30]; b=[20:2:40]; c(i)=a(i)-b(i); if c(i)<0; disp error; end end So, I was hoping it would put in a stop point when i=2...but it doesn't...even though it is displaying the 'error'. Am I doing something stupid? – matlab_newby Feb 11 '14 at 13:19
  • @matlab_newby but there is no error when `i=2`. try `assert(c(i)>=0)` just after your `if..end` – Dan Feb 11 '14 at 13:27
  • I think the issue is that you are calling `dbstop in 'db_test.m' if error` within your loop within `db_test.m` itself. Instead, run `dbstop in 'db_test.m' if error` from the command window and then run the file – you might need to run `dbclear db_test.m` first. – Bill Cheatham Feb 11 '14 at 13:30

1 Answers1

1

You could try dbstop if error and then in your code assert(var>=0) at the point where you think it might be becoming negative (i.e. throw an error if it is not)

Dan
  • 45,079
  • 17
  • 88
  • 157
  • I've never used the 'assert' command before..it could be useful..but I have no idea where the variable is becoming negative. It is nested within a nested loop. I'll look into that command though a bit more. Thanks – matlab_newby Feb 11 '14 at 13:23
  • How often does it change values (in how many lines of code) within those loops? Or else just place it at the end of a loop and then you can check the loop state and set a normal conditional break point – Dan Feb 11 '14 at 13:25
  • it changes value daily for 140 years of daily input. I can't place it at the end of the loop either due to the fact that other variables are dependant on its value (which needs to be calculated first). If I could place it at the end though; what do you mean by 'set a normal conditional breakpoint'?'dbstop if error'? – matlab_newby Feb 11 '14 at 13:42
  • You can place it at the end. I mean when you enter debug mode, check the values of the looping variables and then set a [conditional breakpoint](http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-234) to stop at the beginning of the loop at those values so you can just step through that one loop iteration where your variable goes negative and see exactly where it happens. – Dan Feb 11 '14 at 13:48
  • But when I said how often does it change, I mean how often in terms of lines of code. How many times do you assign or alter it's value? Just put the assert directly after those lines. I doubt it will be many. – Dan Feb 11 '14 at 13:49
  • that worked. That's great.As you said, I put in the assert(var(i)>=0) after the variable had been calculated. Many thanks – matlab_newby Feb 11 '14 at 14:17