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
Scripts are the simplest type of code file, since they store commands exactly as you would type them at the command line. However, functions are more flexible and more easily extensible. To calculate the area of another triangle using the same script, you could update the values of b and h in the script and rerun it.
MATLAB® live scripts and live functions are interactive documents that combine MATLAB code with formatted text, equations, and images in a single environment called the Live Editor. In addition, live scripts store and display output alongside the code that creates it.
Accepted Answer The ability to determine which toolboxes are used in a MATLAB file has been added for MATLAB 6.5 (R13). You can check the licenses that have been checked out by using the LICENSE function.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With