Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is certain matlab-routine used in matlab script?

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

like image 433
Koen Avatar asked Sep 22 '17 06:09

Koen


People also ask

What is the difference between MATLAB script and MATLAB function?

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.

What is the difference between script and live script in MATLAB?

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.

How do you tell what MATLAB toolboxes are used?

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.


1 Answers

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
like image 62
Luis Mendo Avatar answered Nov 15 '22 05:11

Luis Mendo