bastibe / transplant

Transplant is an easy way of calling Matlab from Python
https://transplant.readthedocs.io
Other
110 stars 26 forks source link

Using transplant to import java classes #34

Closed alexjwilliams closed 7 years ago

alexjwilliams commented 7 years ago

Hello,

I am working on a school project where I need to use java classes in MATLAB. In a regular matlab script you can import java classes using the following syntax:import edu.stanford.math.plex4.*;. How can I execute these types of statements using transplant?

Thanks, Alex WIlliams

bastibe commented 7 years ago

I have honestly never tried this. It seems kind of convoluted to use Python to tell Matlab to load Java code. That said, import is just a regular function, so import('edu.stanford.math.plex4.*') should work.

alexjwilliams commented 7 years ago

That doesn't work because import is a reserved keyword in Python. I figured out a way around this (by putting the import statement in the matlab code, and writing more logic in matlab than in Python), so I don't need any more help, but others in the future might want to be able to call Matlab import from Python. Just letting you know.

bastibe commented 7 years ago

Thank you for bringing this up.

You can always use Python's getattr if the keyword is taken:

import transplant
m = transplant.Matlab()
importfunc = getattr(m, 'import')

And then use importfunc instead of import.