Here’s a neat way to keep track of the cost, i.e. number of function calls in Matlab: rather than using a counter variable and increment it each time a function is called, use the internal Matlab counters, accessible through the FunctionTable struct array.
The loop below is necessary only to get the index of the desired function in the struct array (If you know a better way of looking up a function in FunctionTable, let me know!).
Replace the function name ftest if needed.
profile on
% your code comes here
% (cost is counted from "profile on" to "profile off")
p = profile('info');
for i = 1:size(p.FunctionTable,1)
if strcmp(p.FunctionTable(i).FunctionName,'ftest')
break
end
end
cost = p.FunctionTable(i).NumCalls;
profile off
Function call counting in Matlab
Here’s a neat way to keep track of the cost, i.e. number of function calls in Matlab: rather than using a counter variable and increment it each time a function is called, use the internal Matlab counters, accessible through the FunctionTable struct array.
The loop below is necessary only to get the index of the desired function in the struct array (If you know a better way of looking up a function in FunctionTable, let me know!).
Replace the function name ftest if needed.
profile on % your code comes here % (cost is counted from "profile on" to "profile off") p = profile('info'); for i = 1:size(p.FunctionTable,1) if strcmp(p.FunctionTable(i).FunctionName,'ftest') break end end cost = p.FunctionTable(i).NumCalls; profile off