Is there a way to record the usage of functions in a matlab program?

12 visualizzazioni (ultimi 30 giorni)
I am distributing a Matlab programm with a variety of function. I would like to record, how often a user has invoked a specific function. So, I want to get some information like: User 1 used function A 12 times today. Is there any way possible to achieve this? Thanks in advance.
  3 Commenti
Adam Danz
Adam Danz il 25 Apr 2019
Yeah, or getenv('computername') but this doesn't help when people are using public computers.

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 25 Apr 2019
Modificato: Adam Danz il 25 Apr 2019
Here's a way to store the timestamp and user name to a mat file every time a function is called. The username is pulled from the system's operating environment.
This function should be called from within each function you're tracking. It identifies the caller function and stores its name along with a timestamp and username to a mat file.
function functionCounter()
% functionCounter() records the name of the function caller along with a timestamp and username,
% all stored in a cell array within a mat file.
% https://www.mathworks.com/matlabcentral/answers/458517-is-there-a-way-to-record-the-usage-of-functions-in-a-matlab-program
filename = 'functionCounterData.mat';
if exist(filename, 'file')==0
Counter = {};
save(filename, 'Counter', '-v7.3')
end
db = dbstack;
stack = {db.name};
m = matfile(filename, 'Writable', true);
m.Counter(end+1,1:3) = {stack{min(2,numel(stack))}, now(), getenv('username')}; %or getenv('computername')
end
Now you just need to insert this line of code somewhere in each function you'd like to track
functionCounter();
To analyze the time stamps, just load the functionCounterData.mat file into the matlab workspace and look at the variable 'Counter' which will be a [n-by-3] cell array where column 1 is a list of function names and column 2 are time stamps and column 3 are user names.
This will add processing time and will slow down execution - especially as the dataset grows. Some functions are potentially called 100s of times within a couple seconds so this dataset can potentially grow very quickly.
You must add a notification to users that their username is being tracked as Guillaume mentioned. https://eugdpr.org/

Più risposte (1)

Guillaume
Guillaume il 25 Apr 2019
Modificato: Guillaume il 25 Apr 2019
Bearing in mind that as Adam says matlab has no concept of a user and also bearing in mind that collecting user names may fall under GDPR or equivalent laws, you can collect function usage information with the profiler. With -history on, the profiler collects which function called which. Bear in mind that having the profiler on will impact performance.
If that's not what you want, then you'll have to modify your function so that it keeps track itself of who calls it.

Prodotti


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by