Matt Mizumi
Matt Mizumi

Reputation: 1193

How to track function calls in MATLAB?

I would like a way to track all function calls that have operated on a specific workspace variable -- for instance, a sound waveform that will be transformed by various signal processing functions.

One cumbersome and fragile way is to do this:

>> cfg = [];
>> curr_call = 'data_out = my_function(cfg,data_in);';
>> eval(curr_call);
>> data_out.cfg.history = cat(1,data_out.cfg.history,{curr_call});

What would be much better is the following:

>> cfg = [];
>> data_out = my_function(cfg,data_in);
>> data_out.cfg.history

  'data_out = my_function(cfg,data_in);'

EDIT for clarification: In other words, this variable has a field, cfg.history, that keeps track of all history-enabled functions that have operated on it (ideally with arguments). The history field should be updated regardless of where function calls originate: my example above is from the command line, but calls made from cell mode or within a script should also be appended to the history. Obviously, I can edit my_function() in the above example so that it can modify the history field.

NOTE in response to discussion below: the motivation for doing this is to have the history "attached" to the data in question, rather than say, in a separate log file which would then need to be packaged with the data somehow.

Can this be done?

Upvotes: 4

Views: 1266

Answers (1)

Daniel
Daniel

Reputation: 36710

You can access the full Session history using this code:

import com.mathworks.mlservices.MLCommandHistoryServices
history=MLCommandHistoryServices.getSessionHistory;

To achive what you want, use this code:

import com.mathworks.mlservices.MLCommandHistoryServices;
startcounter=numel(MLCommandHistoryServices.getSessionHistory);
disp('mydummycommand');
disp('anotherdummycommand');
history=MLCommandHistoryServices.getSessionHistory;
commands=cell(history(startcounter-2:end-1));

Be aware that these functions are undocumented. It uses the command history which is typically located at the bottom right in your matlab.

Upvotes: 2

Related Questions