How to profile only user created functions in MATLAB

Let's suppose you want to profile your MATLAB code, but you get the information over all the functions, MATLAB built-in and yours. This tutorial will show how to get information only about your functions, discarding the MATLAB built-in ones.

Use the 'info' parameter of profile function

You will have to use the 'info' parameter of the profile function, and implement some logic around it. You will have to discard the built-in MATLAB functions by comparing their path names with matlabroot.

Here is an example of how you can do that, in your MATLAB console:

profile on
yourMatlabFunction

% Stop the profiler and save the stats info
profile off
statsInfo = profile('info');

% Sort the results by the total execution time
[~,I] = sort([statsInfo.FunctionTable.TotalTime], 'descend');

for i = I

% Get the files structure
files = statsInfo.FunctionTable(i);

% Discard the built-in Matlab functions
if ~isempty(findstr(files.CompleteName, matlabroot))
continue;
end

% Print the execution time and the function name
fprintf('%.06f sec\t', files.TotalTime);
fprintf('%s\n', files.FunctionName);

end

Other articles:

How to save the results of profile in MATLAB

How to compute mean value, ignoring NaN values in MATLAB