Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple matlab function to solve the dependencies for a function. Might be useful when deploying a single function from a larger package.
function files = findRequiredFunctions(mainFun)
% FINDREQUIREDFUNCTIONS- solve file dependencies
% function files = findRequiredFunctions(mainFun)
% Find ALL files that are called by as function
% provided as MAINFUN, which are not part of the
% default Matlab installation.
% The general behaviour is covered by DEPFUN, but
% this will return a huge amount of files and take
% quite a long time to run. This implementation only
% queries the topmost dependent functions, but his
% recursively.
% Input:
% fun = name of function
% Output:
% files = cell array with all files
% Requires: depfun
% See also: --
if nargin < 1
% the function of interest
% find top only depencies
fileList = depfun(mainFun,'-toponly','-quiet');
% aquire indeces of all files not in matlabpath
res = cellfun(@(x) strfind(x,matlabroot) ,fileList,'UniformOutput',0);
idx = find( cellfun(@isempty,res) );
% assing match files to output
% the first hit always matches input
files = {};
files = [files fileList{ idx(2:end) }];
% loop over found files
for iRes = 2:numel(idx)
% recursivly loop over input until all files are matched
fprintf('Working on %s \n',fileList{idx(iRes)});
files = [files findRequiredFunctions(fileList{idx(iRes)}) ];
end %iRes
% unique output, since some functions may
% call the same function
files = unique(files);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment