MATLAB Answers

When a Matlab function block running inside a simulink model gives an error, how can I find out which line the error occurred on?

32 views (last 30 days)
Anderson
Anderson on 31 May 2013
When running a Simulink model that contains an "Interpreted Matlab Function" block, if an error occurs inside the Matlab function there seems to be no easy way to find out what line of the function gave the error. When the error occurs, a Simulink Diagnostics window pops up telling me what error occurred and which block it occurred in. But is doesn't say what line of the function caused the error.
This is a problem I encounter frequently, and it is difficult to debug. For instance, it just occurred about a half hour into a simulation. What I'll probably do is start outputting a bunch of values to the command window so I can get an idea where in the function the error occurred, but I can take a very long time to find the error this way when it occurs far along in the simulation.
It seems like Matlab must know what line caused the error, right? How can I get it to tell me?
Thanks for any help!

  0 Comments

Sign in to comment.

Accepted Answer

Anderson
Anderson on 31 May 2013
Okay, a co-worker (Alexander Brissette) came up with a good solution using try-catch. It works like this:
try
<code that might produce error>
catch err
err
err.stack
line = err.stack(1).line
end
This prints the error, filename, and line number. You can also put a breakpoint in the "catch" block so you can see all your variables when an error occurs. You could put an entire function inside the try block. And it works even when the function is called from within Simulink!

  0 Comments

Sign in to comment.

More Answers (2)

David Sanchez
David Sanchez on 31 May 2013

  3 Comments

Anderson
Anderson on 31 May 2013
Thanks, but it is not clear to me which of these functions you are suggesting I try.
If you're suggesting breakpoints, I do use them frequently. However when calling a function from Simulink the function may run thousands of times before giving an error. So what I'll often do is put a breakpoint inside an {if} statement so it only breaks on the timestep where the error occurs, but this is still very time consuming. It seems there should be a simple way for Matlab/Simulink to tell me which line caused the error, just like it does when you're running a function normally (without using Simulink).
David Sanchez
David Sanchez on 31 May 2013
For what you say, it seems your code has to be right, does any signal become complex during runtime? What messages do you receive from simulink?
Anderson
Anderson on 31 May 2013
The two simulink error messages are: Error in Interpretted Matlab Function block and Matrix dimension must agree
Unfortunately there are at least several hundred matrix operations in the function that gives the error, so it's a tedious process to narrow down.
If you mean "complex" as in (real+imaginary), no, there are no complex signals.

Sign in to comment.


Kaustubha Govind
Kaustubha Govind on 31 May 2013
You could try using dbstop if error to see if the execution breaks when the error occurs.

  1 Comment

Anderson
Anderson on 31 May 2013
That seemed like a good idea, but it didn't work. It appears that "dbstop if error" is ignored when a function is called from within Simulink. I do not "clear all" before running, so I'm not sure why Simulink would ignore the dbstop....

Sign in to comment.

Sign in to answer this question.


Translated by