问题
I am running a big m-file that I didn't write myself and that depends on certain subfunctions. I want to know if anywhere in all nested functions a particular function (in my case the function eig.m (to calculate eigenvalues) ) is used. Is there a quick way to do this?
kind regards, Koen
回答1:
You can use the semi-documented function getcallinfo
(see Yair Altman's blog for more information about it):
getcallinfo
Returns called functions and their first and last lines
This function is unsupported and might change or be removed without notice in a future version.
General use of getcallinfo
Let's create an example script which contains subfunctions (this works in Matlab R2016b or newer) and save it as 'filename.m
'. The procedure also works if there are nested functions, or if the main file is a function instead of a script.
x = input('');
y = find(x);
z = f(norm(x));
disp(z)
function u = f(v)
u = -log2(v) + log2(pi);
end
Then:
>> g = getcallinfo('filename.m');
gives you a nested struct array with interesting information, including function calls. The first entry, g(1)
, refers to the main file. There may be further entries for subfunctions or nested functions. In this case, g(2)
refers to subfunction f
.
>> g(1).calls.fcnCalls
ans =
struct with fields:
names: {'input' 'find' 'norm' 'disp' 'log2' 'log2' 'pi'}
lines: [1 2 3 4 6 6 6]
>> g(1).calls.innerCalls
ans =
struct with fields:
names: {'f'}
lines: 3
>> g(2).calls.fcnCalls
ans =
struct with fields:
names: {'log2' 'log2' 'pi'}
lines: [6 6 6]
>> g(2).calls.innerCalls
ans =
struct with fields:
names: {1×0 cell}
lines: [1×0 double]
Other fields of g
give further details, such as name
>> g(1).name
ans =
filename
>> g(2).name
ans =
f
or type
>> g(1).type
ans =
Script with no properties.
>> g(2).type
ans =
subfunction
How to determine if a given function is used anywhere in the file
Obtain g
as explained above, and then look for the desired function name in all calls.fcnCalls.names
fields of g
:
g = getcallinfo('filename.m');
sought_function = 'log2'; % or 'eig' in your case
t = arrayfun(@(x) x.calls.fcnCalls.names, g, 'UniformOutput', false);
% collect all names of called functions. Gives a cell array of cell arrays
% of strings (character vectors)
t = [t{:}]; % de-nest: concatenate into cell array of strings
result = any(strcmp(t, sought_function)); % compare with sought function name
回答2:
One option to check by hand would be to use the profiler.
I'm using a 2014 Matlab, i cannot run the previous example. However, as far as i saw on the previous answer by Luis Mendo, it shows similar information but in a webpage style.
回答3:
Basically, I would also suggest you use the profiler, which will list all called functions.
But to give you an alternative, if you are looking for just a single function (please see the disclaimer), you can utilize that functions in the current folder have precedence. Thus, say that you want to check if you use a function called jacobianest
, then you create a new function in your folder called jacobianest.m
with e.g.
function jacobianest(args)
error('jacobianest is being used')
end
then if the program terminates with that error, you are using jacobianest
.
Disclaimer 1: This works only for the non-build-in functions. In the sense that if you type edit *name of function*
and only comments come up, you cannot use this strategy. If the code itself shows up, you can.
来源:https://stackoverflow.com/questions/46358432/is-certain-matlab-routine-used-in-matlab-script