Can MATLAB save the Profiler results table (Function Name, Calls, etc.) to a text file or a spreadsheet?

Can MATLAB save the Profiler results table (Function Name, Calls, Total Time, Self Time) to a text file or a spreadsheet?
I need this to merge and compare Profiler results from different software versions.

15 Comments

You can use profile('info'), and then write what you need to a file. profsave also can save it to an HTML doc.
With INFO = profile('info'), INFO.FunctionTable looks relevant. How do I extract the information from it though?
I cannot tell how it is stored: class(INFO.FunctionTable.FunctionName) gives "Error using class. The CLASS function must be called from a class constructor."
Can you tell me how to extract FunctionName, TotalRecursiveTime and TotalTime so I can write them to a file?
Thanks!
Or any other way to get the result to a text file. Thanks!
I guess you can do something like this
profile on
% your code
info = profile('info');
profile viewer % to compare
infotab = struct2table(info.FunctionTable);
infotabMain = infotab(:, ["FunctionName", "NumCalls", "TotalTime"]); % main table in profile viewer
infotabExc = cell(height(infotab), 1); % execution time per each subfunction
for i = 1:height(infotab)
infotabExc{i} = infotab.ExecutedLines{i};
infotabExc{i} = array2table(infotabExc{i}, ...
'VariableNames', {'Line Number', 'Calls', 'Total time(s)'});
end
I hit a glitch:
>> infotabMain = infotab(:, ["FunctionName", "NumCalls", "TotalTime"]); % main table in profile viewer
Table variable subscripts must be real positive integers, logicals, character vectors, or cell arrays of character vectors.
---------
I don't know enough to fix this. Can you help me until it works? Thanks!
PS The variables I seek are: FunctionName, TotalRecursiveTime and TotalTime. Thanks!
I get no error:
info = load("info.mat").info;
infotab = struct2table(info.FunctionTable);
infotabMain = infotab(:, ["FunctionName", "NumCalls", "TotalTime", "TotalRecursiveTime"]); % main table in profile viewer
infotabExc = cell(height(infotab), 1); % execution time per each subfunction
for i = 1:height(infotab)
infotabExc{i} = infotab.ExecutedLines{i};
infotabExc{i} = array2table(infotabExc{i}, ...
'VariableNames', {'Line Number', 'Calls', 'Total time(s)'});
end
% show me what you got
head(infotabMain)
ans = 8×4 table
FunctionName NumCalls TotalTime TotalRecursiveTime ______________________________________ ________ _________ __________________ {'Nexus_Ev3' } 1 43.706 0 {'clear_M' } 1 0.0053484 0 {'close' } 4 0.056067 0 {'close>getEmptyHandleList' } 4 0.0009194 0 {'allchild' } 12 0.018321 0 {'uitools\private\allchildRootHelper'} 12 0.0013167 0 {'createWorkbook>initLibrary' } 14 0.0026479 0 {'findall' } 22 0.04518 0
size(infotabExc)
ans = 1×2
1133 1
head(infotabExc{1})
ans = 8×3 table
Line Number Calls Total time(s) ___________ _____ _____________ 1 1 0.034877 4 1 0.0001737 6 1 1.82e-05 7 1 1.71e-05 8 1 1.64e-05 9 1 1.53e-05 10 1 1.52e-05 12 1 0.0001247
You have R2021b but I have: MATLAB Version: 9.3.0.948333 (R2017b) Update 9.
Your syntax for loading info.mat does doesn't work for me. I can however load it:
>> load('C:\Users\Leigh\Documents\Professional\Mayfield\Nexus\Funds\Ev3\info.mat')
>> fieldnames(info)
ans =
6×1 cell array
{'FunctionTable' }
{'FunctionHistory'}
{'ClockPrecision' }
{'ClockSpeed' }
{'Name' }
{'Overhead' }
Moving on, I get the same error as before:
infotab = struct2table(info.FunctionTable);
>> infotab = struct2table(info.FunctionTable);
>> infotabMain = infotab(:, ["FunctionName", "NumCalls", "TotalTime", "TotalRecursiveTime"]); % main table in profile viewer
Table variable subscripts must be real positive integers, logicals, character vectors, or cell arrays of character vectors.
The results you show look like exactly what I need. Is it possible the different MATLAB versions have different syntaxes? Do you have access to R2017b?
Thank you!
Ooops, my bad!
What about this?
info = load("info.mat").info;
infotab = struct2table(info.FunctionTable);
keepCol = ismember(infotab.Properties.VariableNames, ["FunctionName", "NumCalls", "TotalTime", "TotalRecursiveTime"]);
infotabMain = infotab(:, keepCol); % main table in profile viewer
infotabExc = cell(height(infotab), 1); % execution time per each subfunction
for i = 1:height(infotab)
infotabExc{i} = infotab.ExecutedLines{i};
infotabExc{i} = array2table(infotabExc{i}, ...
'VariableNames', {'Line Number', 'Calls', 'Total time(s)'});
end
% show me what you got
head(infotabMain)
ans = 8×4 table
FunctionName TotalRecursiveTime NumCalls TotalTime ______________________________________ __________________ ________ _________ {'Nexus_Ev3' } 0 1 43.706 {'clear_M' } 0 1 0.0053484 {'close' } 0 4 0.056067 {'close>getEmptyHandleList' } 0 4 0.0009194 {'allchild' } 0 12 0.018321 {'uitools\private\allchildRootHelper'} 0 12 0.0013167 {'createWorkbook>initLibrary' } 0 14 0.0026479 {'findall' } 0 22 0.04518
I cannot see an "Accept this answer" button for you, only for the other contributor. How do I accept your answer and/or vote for you?

Sign in to comment.

 Accepted Answer

info = load("info.mat").info;
infotab = struct2table(info.FunctionTable);
infotabMain = infotab(:, ["FunctionName", "NumCalls", "TotalTime", "TotalRecursiveTime"]); % main table in profile viewer
infotabExc = cell(height(infotab), 1); % execution time per each subfunction
for i = 1:height(infotab)
infotabExc{i} = infotab.ExecutedLines{i};
infotabExc{i} = array2table(infotabExc{i}, ...
'VariableNames', {'Line Number', 'Calls', 'Total time(s)'});
end

More Answers (2)

Yes, it can be done realtively easy with profsave(profile()), e.g.:
profile on
x = linspace(0, 1, 2e3);
y = sin(2*pi*t)+randn(size(x));
plot(x,y), grid on
profile off
% All html files are saved in your current directory and inside a folder called: MY_Profile_results
profsave(profile('info'),'MY_Profile_Results')
You can try this fcn - extracthmltext() to extract the data from html formatted file:
https://www.mathworks.com/help/textanalytics/ref/htmltree.extracthtmltext.html

1 Comment

Sounds like a lot of text manipulation would be needed. I'll wait to see what Ive J comes up with. Thanks though!

Sign in to comment.

Products

Release

R2017b

Asked:

on 21 Feb 2022

Commented:

on 1 Mar 2022

Community Treasure Hunt

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

Start Hunting!