Effacer les filtres
Effacer les filtres

Use pcode in combination with packages

3 vues (au cours des 30 derniers jours)
Steven Lulich
Steven Lulich le 22 Déc 2016
Commenté : Steven Lulich le 26 Jan 2017
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!

Réponse acceptée

Jan
Jan le 5 Jan 2017
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.
  1 commentaire
Steven Lulich
Steven Lulich le 26 Jan 2017
Thanks, Jan. I did not know about \private folders before, but they provide a nice solution for my problem. For the record, the situation in my case is entirely legal and ethical, although you are right to bring up the issue.

Connectez-vous pour commenter.

Plus de réponses (1)

Prashant Arora
Prashant Arora le 29 Déc 2016
Hi Steven,
MATLAB does not provide the ability to call a function in a package without using the package name prefix. However, to solve the issue you are facing, you can use a "hacky" solution explained below.
Let's assume the p-code function (myfcn2.p) calls at least one function (let's say "dummy") on MATLAB search path. You can include the following command in the "dummy" function:
assignin('caller','myfcn1',@pkg.myfcn1);
This essentially creates a local variable 'myfcn1' in the function workspace of "myfcn2.p", which holds the function handle to the original function. As "myfcn1" is called in "myfcn2.p", the correct call is made using the stored function handle.
Note that it is best to choose a "dummy" function which is called only by "myfcn2.p", or you may end up creating the wrong variables at the wrong places.
  1 commentaire
Steven Lulich
Steven Lulich le 5 Jan 2017
Thanks, this is interesting and potentially helpful in the future. For now, though, I don't have access to any such "dummy" function, so it unfortunately doesn't work as a solution to the problem I'm currently facing.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by