Dynamically specify namespace/package

8 visualizaciones (últimos 30 días)
J
J el 18 de Jun. de 2014
Editada: Jacob Halbrooks el 18 de Jun. de 2014
I am trying to write a generic function to preform a calculation using the appropriate namespace/package as specified by the input parameters. Obviously I could do this using eval, however this is quite clumsy and I'm hoping there is a better way. I thought import would work, as the documentation says imports within a function effect only the workspace/import list of that function, but alas this is not the case.
I have included example code below, as well as a zip file with the folder structure intact.
Example:
I have two packages pkg1 and pkg2 each of which implements a function, commonFunction
%pkg1/commonFunction
function commonFunction
disp('Using pkg1')
end
%pkg2/commonFunction
function commonFunction
disp('Using pkg2')
end
function testPackages(packageNameString)
import(packageNameString)
commonFunction
end
%Script to use testPackages
clear all
id1 = 'pkg1.*';
id2 = 'pkg2.*';
testPackages(id1) % output Using Package 1
testPackages(id2) % output Using Pacakge 1
clear import
testPackages(id2) % output Using Package 1
testPackages(id1) % output Using Pacakge 1
clear all
id1 = 'pkg1.*';
id2 = 'pkg2.*';
testPackages(id2) % output Using Package 2
testPackages(id1) % output Using Pacakge 2
%%What if we import the exact function?
clear all
id1 = 'pkg1.commonFunction';
id2 = 'pkg2.commonFunction';
testPackages(id1) % output Using Package 1
testPackages(id2) % output Using Pacakge 1
clear import
testPackages(id2) % output Using Package 1
testPackages(id1) % output Using Pacakge 1
clear all
id1 = 'pkg1.commonFunction';
id2 = 'pkg2.commonFunction';
testPackages(id2) % output Using Package 2
testPackages(id1) % output Using Pacakge 2

Respuestas (1)

Jacob Halbrooks
Jacob Halbrooks el 18 de Jun. de 2014
Editada: Jacob Halbrooks el 18 de Jun. de 2014
A few pieces of documentation explain the behavior where the import list for the function does not update. From the IMPORT doc:
The import list of a function is persistent across calls to that function and is only cleared when the function is cleared.
While that page suggests using "clear import", the doc page for Importing Classes states:
You can not clear the import list from a function workspace. To clear the base workspace only, use: clear import
As a result, only clearing the function clears its import list.
I would suggest you pursue object-oriented programming in order to get the polymorphism it appears you want. For example, create the following two classes:
  • MyClass1.m:
classdef MyClass1 < handle
methods
function commonFunction(obj)
disp('Using MyClass1')
end
end
end
  • MyClass2.m:
classdef MyClass2 < handle
methods
function commonFunction(obj)
disp('Using Myclass2')
end
end
end
Now you can essentially use an object to dispatch to the appropriate implementation of commonFunction:
>> id1 = MyClass1;
>> id2 = MyClass2;
>> commonFunction(id1)
Using MyClass1
>> commonFunction(id2)
Using Myclass2

Community Treasure Hunt

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

Start Hunting!

Translated by