MATLAB: Use pcode in combination with packages

addpathpackagespathpcode

Hello!
I have developed a toolbox for biomedical image (e.g. ultrasound) analysis. The toolbox includes several packages, and although most of the code in the toolbox is open source, there are some important p-code files as well, for which I do not have the source m-code. I would like to avoid addpath commands in this toolbox, but the interaction of p-code with packages is making this difficult.
Here's a simplified example:
% CD to the Desktop (assuming a Windows OS):
>> cd C:\Users\me\Desktop;
% Create a package and cd into it:
>> mkdir('+pkg');
>> cd('+pkg');
% Edit a new function:
>> edit myfcn1.m;
Here is the code for myfcn1:
function y = myfcn1(x);
y = x.^2;
return;
Now edit another new function:
>> edit myfcn2.m;
Here's the code for myfcn2:
function z = myfcn2(x);
z = myfcn1(x);
return;
Now cd back outside of the +pkg directory and call myfcn2 to get the square of the number 2:
>> cd ..
>> z = pkg.myfcn2(2)
Undefined function or variable 'myfcn1'.
Obviously, this doesn't work. Now change myfcn2 as follows:
function z = myfcn2(x);
z = pkg.myfcn1(x);
return;
Try the function call again:
>> z = pkg.myfcn2(2)
z =
4
Great, it works! But….
Change myfcn2.m back to the way it was:
function z = myfcn2(x);
z = myfcn1(x);
return;
And now convert this function to p-code:
>> pcode myfcn2.m;
Now throw away the myfcn2.m file and keep only myfcn1.m and myfcn2.p.
We're back to the original problem:
>> z = pkg.myfcn2(2)
Undefined function or variable 'myfcn1'.
Basically, the call to myfcn1 from within myfcn2 assumes that myfcn1 is on the MATLAB path rather than in a package, and I can't change myfcn2 because it is p-code.
One solution is to not use a package for these functions and make sure that they are on the MATLAB path. But for a variety of reasons I'd rather keep them packaged together. Is there a way to do this?
Thanks very much!

Best Answer

Ask the author of the P-file, if he creates a package version for you. This might involve some payment, but this is at least legal, while spoofing function handles inside the P-file might look like a kind of reverse engineering.
In you example you can move myfcn1 to the subfolder \private . Then myfcn2 recognizes it. You can use this by creating a set of all M-files "xyz.m" which forward all calls to the package-functions:
\private\xyz.m:
function varargout = xyz(varargin)
[varargout(1:nargout)] = pkg.xyz(varargin{:});
Perhaps this causes confusions, if the P-coded function calls itself recursively, but it is worth to try.