catching a value while matlab is running

8 views (last 30 days)
Dimitrios on 21 Oct 2014
Edited: per isakson on 22 Oct 2014
I am building a simulation with many functions using OOP(beginner). Sometimes I have some values from different variables that are not desirable but I can not see them while the simulation is running. So I was wondering if there is any way to catch this value while the simulation is running. I believe it could be done inside the functions but there so many that its inefficient and time-consuming to do it. Can addlisterer be used for that or any other way? Any suggestions?
Thanks in advance
Dimitrios on 22 Oct 2014
Thanks for the reply.The names of the variables are known.Same name in many functions.I ll try to use addlistener for first time then.(btw I cant find any video tutorial for that,only the example with the bank from the documentation.Anything else out there?) :p

Sign in to comment.

Answers (2)

Robert Cumming
Robert Cumming on 22 Oct 2014
you could use listeners, but you could also use the dbstop ability to stop on condition, i.e.
where Expression is of the format:
myVariable > 1000
You can also add these conditionss interactively in the editor by right clicking on the line number and adding a Conditional Breakpoint.
By adding these conditions your code will stop if the condition is true and you can debug from there.

per isakson
per isakson on 22 Oct 2014
Edited: per isakson on 22 Oct 2014
The documentation on Events and Listeners is okay and I didn't find any examples with banks. However, I had to run and modify the examples to really get it. See
I doubt that Events and Listeners is a good approach (to spy om a simulation). It takes some extra code to use Events and Listeners to peek on a running simulation. You will have to either make all the properties (variables) Observable or litter the code with notify. And I guess the performance will be hurt.
You need to make some serious experiments before you start adding code to 100 methods.
An alternative approach builds on the answer by Robert Cumming: &nbsp dbstop if error .... &nbsp I have a hammer and see a nail in your question :)
Building blocks
  • enable_spying.m &nbsp is a script, which contains statements like: &nbsp dbstop in mfile at lineno if my_spy( variables_of_interest ). &nbsp It will be hundreds similar statements.
  • my_spy &nbsp always returns false. It shall not interrupt the simulation.
  • my_spy &nbsp could cause all sorts of side effects, which is the trick: &nbsp make test on the input arguments; add point to a diagram; call a logger (e.g. log4m); etc.
  • log4m - A powerful and simple logger for matlab, by Luke Winslow (and there a few more loggers in the File Exchange)
  • ALWAYSONTOPby Elmar Tarajan to keep the diagram visible
  • tracer4m excels in the use of side effects of &nbsp dbstop in mfile at lineno if foo
  • run the script, enable_spying.m
  • start your simulation
  • watch the output file of log4m with an editor. I use notepad++
  • not a single line of extra code in your simulation program
  • simple to switch on and off the spying feature
  • does not affect the performance when switched off
  • non-standard. However, it is based on documented features of Matlab.
  • the script, enable_spying.m, must be edited to reflect changes in the code of the simulation program
  • and more - I guess


Find more on Debugging and Analysis 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!