Import the same class around the world

I have a function file called getFeatures.m that looks like this:

 function [features] getFeatures() % Import the XPath classes import javax.xml.xpath.* % other code end function [name] = getName() % Import the XPath classes import javax.xml.xpath.* % other code end 

As you can see, both functions import the xpath library, since I have many functions that should import this class, how can I do this at a time?

+6
source share
1 answer

I came across the same problem. My personal (and ugly!) Workaround for this is to identify the method that imports; you still have to call this function, but at least it groups the import in one place, albeit inside the lines.

 function cmd = initJava() cmd = 'import package.*'; if nargout == 0 warning('off','MATLAB:Java:DuplicateClass'); evalin('caller',cmd); warning('on','MATLAB:Java:DuplicateClass'); end; end 

This can be called either initJava() or eval(initJava()) . If I remember correctly, the former does not always do what it should do, but you will have to check it yourself.

If anyone has a better / better / different solution, I am very interested to hear that.

+2
source

Source: https://habr.com/ru/post/899438/


All Articles