catching a value while matlab is running
8 views (last 30 days)
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
Robert Cumming on 22 Oct 2014
you could use listeners, but you could also use the dbstop ability to stop on condition, i.e.
dbstop in MFILE at LINENO if 'EXPRESSION'
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 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:   dbstop if error ....   I have a hammer and see a nail in your question :)
- enable_spying.m   is a script, which contains statements like:   dbstop in mfile at lineno if my_spy( variables_of_interest ).   It will be hundreds similar statements.
- my_spy   always returns false. It shall not interrupt the simulation.
- my_spy   could cause all sorts of side effects, which is the trick:   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   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