File Exchange Pick of the Week

Our best user submissions

Trace your calls (to your methods)

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.




Published with MATLAB® R2017a

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.