File Exchange Pick of the Week

Our best user submissions

Trace your calls (to your methods) 1

Posted by Jiro Doke,

Jiro‘s pick this week is tracer4m by per isakson.

I teach a training course on “Object-Oriented Programming (OOP) with MATLAB”, and I just taught the course this week. Half of the students were new to the concept of OOP, while the other half had programmed in OOP languages like C++ and C#. One of the concepts I find fascinating, and thus love to teach, is events and listeners. The idea of each object having their own responsibilities and commicating via triggering events makes the program easier to maintain. At the same time, many new-comers find it difficult to follow the chain of events that are intricately intertwined. Sometimes you don’t realize a certain operation has occurred because on the surface it seems unrelated to the task at hand. But it happened to be some action that occurred as a result of a cascade of events. Debugging such related actions can sometimes be difficult. Adding breakpoints and stepping through in the debugger doesn’t let you easily capture the timing of the chain of events.

This is where per’s tracer4m comes into play. He brilliantly uses conditional breakpoints and sneaks in instrumentation to capture entry and exits to functions and methods. I tested it with an OOP example I showed in last year’s MATLAB Expo. It has some events and listeners as well as handle graphics callbacks.

This is how it works. I just create a tracer instance.

log = TraceHistory.Instance;

Specify which files I want to monitor.

setup(log,{fullfile(pwd,'@Data1d','Data1d'),...
    fullfile(pwd,'@Data1d','interpolate'),...
    fullfile(pwd,'@Data1d','plot')})

This adds conditional breakpoints at the beginning and end of all methods and functions in those files.

Then I just do my normal operation with the app.

After some time, I just check the log.

disp(log)
subsref
    Data1d.get.X
subsref
    Data1d.get.Data
addDraggablePoints/movePt
    plot/updateDataPlot
        Data1d.get.X
        Data1d.get.Data
        plot/updateInterpPlot
            Data1d.get.X
            Data1d.get.X
            interpolate
                Data1d.get.X
                Data1d.get.Data
subsref
    Data1d.get.X
subsref
    Data1d.get.Data
addDraggablePoints/movePt
    plot/updateDataPlot
        Data1d.get.X
        Data1d.get.Data
        plot/updateInterpPlot
            Data1d.get.X
            Data1d.get.X
            interpolate
                Data1d.get.X
                Data1d.get.Data
              .
              .
              .

This repeats over and over again. Basically, as I moved my mouse, a series of methods were being called to update the graphics.

Very nice!

Comments

Give it a try and let us know what you think here or leave a comment for per.


Get the MATLAB code

Published with MATLAB® R2017a

Note

Comments are closed.

1 CommentsOldest to Newest

Yair Altman replied on : 1 of 1

I apologize in advance for the self-promotion, but in 2014 I posted a File-Exchange utility that displays all the timing information logged by the built-in Profiler, more importantly the order of function calls invocation (which is not reported by the built-in profiler report), and the duration of each specific invocation (as opposed to just their total as reported by the profiler report), in a graphical manner using an interactive GUI:

https://www.mathworks.com/matlabcentral/fileexchange/46976-profile-history-display-graphical-profiling-timeline-data

In addition to being graphical/interactive, the utility has the benefit of logging all function calls (including built-in ones), as well as reporting timing information in an intuitive visual manner, with interactive GUI control of the displayed detail (zoom level).