DarkCell
DarkCell

Reputation: 180

MATLAB organise external toolboxes or turn them into packages to prevent shadowing

I'm working on a large data analysis that incorporates lots of different elements and hence I make heavy use of external toolboxes and functions from file exchange and github. Adding them all to the path via startup.m is my current working method but I'm running into problems of shadowing function names across toolboxes. I don't want to manually change function names or turn them into packages, since a) it's a lot of work to check for shadowing and find all function calls and more importantly b) I'm often updating the toolboxes via git. Since I'm not the author all my changes would be lost. Is there programmatic way of packaging the toolboxes to create their own namespaces? (With as little overhead as possible?) Thanks for the help

Upvotes: 1

Views: 110

Answers (1)

Daniel
Daniel

Reputation: 36710

You can achieve this. Basic idea is to make all functions in a private folder and have only one entry point visible. This entry point is the only file seeing the toolbox, and at the same time it sees the toolbox function first regardless of the order in the search path.

toolbox/exampleToolbox.m

function varargout=exampleToolbox(fcn,varargin)
fcn=str2func(fcn);
varargout=cell(1,nargout);
[varargout{:}]=fcn(varargin{:});
end

with toolbox/exampleToolbox/private/foo.m beeing an example function.

Now you can call foo(1,2,3) via exampleToolbox('foo',1,2,3)

The same technique could be used generating a class. This way you could use exampleToolbox.foo(1,2,3)

Upvotes: 1

Related Questions