Is it possible to import and use third party matlab functions/packages in python?

조회 수: 5 (최근 30일)
I would like to call functions from packages that are imported in Matlab in Python using the MATLAB Engine API for Python. The import/setup work for the package I want to use is contained in load_javaplex.m:
% This script prepares the javaplex library for use
clc; clear all; close all;
%clear import;
javaaddpath('./lib/javaplex.jar');
import edu.stanford.math.plex4.*;
javaaddpath('./lib/plex-viewer.jar');
import edu.stanford.math.plex_viewer.*;
cd './utility';
addpath(pwd);
cd '..';
fprintf('DONE');
% This last line calls a function to test the library
api.Plex4.createExplicitSimplexStream()
In Python I tried the following:
import matlab.engine
eng = matlab.engine.start_matlab()
# prepare javaplex library for use
eng.load_javaplex(nargout=0)
eng.api.Plex4.createExplicitSimplexStream()
When the load_javaplex.m runs it successfully calls api.Plex4.createExplicitSimplexStream(), so we know the package is working in Matlab. But calling eng.api.Plex4.createExplicitSimplexStream() results in an undefined function error (output from running the Python code above):
DONE
ans =
edu.stanford.math.plex4.streams.impl.ExplicitSimplexStream@5e894fec
Undefined function or variable 'api.Plex4.createExplicitSimplexStream'.
Traceback (most recent call last):
File "/Users/alex/PycharmProjects/SeniorResearchProject/Code/sandbox/test2.py", line 6, in <module>
eng.api.Plex4.createExplicitSimplexStream()
File "/Users/alex/anaconda/lib/python2.7/site-packages/matlab/engine/matlabengine.py", line 84, in __call__
_stderr, feval=True).result()
File "/Users/alex/anaconda/lib/python2.7/site-packages/matlab/engine/futureresult.py", line 68, in result
return self.__future.result(timeout)
File "/Users/alex/anaconda/lib/python2.7/site-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError: Undefined function or variable 'api.Plex4.createExplicitSimplexStream'.
Process finished with exit code 1
Is there a way to call these third party functions from Python?

채택된 답변

Robert Snoeberger
Robert Snoeberger 2017년 1월 9일
It looks like your problem is with import. I don't think the import that executes in your script, load_javaplex, is available when you try to call createExplicitSimplexStream. Try using the full name instead of the shortened name. I would guess that the full name is edu.stanford.math.plex4.api.Plex4.createExplicitSimplexStream. Example:
% Use the full name
eng.edu.stanford.math.plex4.api.Plex4.createExplicitSimplexStream()
  댓글 수: 1
Alexander Williams
Alexander Williams 2017년 1월 9일
This does work, but the matlab engine api is unable to handle the custom datatype (a java object) returned by the function call:
Traceback (most recent call last):
File "/Users/alex/PycharmProjects/SeniorResearchProject/Code/sandbox/test2.py", line 8, in <module>
eng.edu.stanford.math.plex4.api.Plex4.createExplicitSimplexStream()
File "/Users/alex/anaconda/lib/python2.7/site-packages/matlab/engine/matlabengine.py", line 84, in __call__
_stderr, feval=True).result()
File "/Users/alex/anaconda/lib/python2.7/site-packages/matlab/engine/futureresult.py", line 68, in result
return self.__future.result(timeout)
File "/Users/alex/anaconda/lib/python2.7/site-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
TypeError: unsupported data type returned from MATLAB
I think I will just keep all calls to the java library in matlab code for now. Since you did technically answer my question I accepted. Thanks for responding!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bo Li
Bo Li 2017년 1월 9일
It looks that "api.Plex4.createExplicitSimplexStream()" is a Java API and it works when it is called by Python Engine as part of load_javaplex.m, while calling eng.api.Plex4.createExplicitSimplexStream() directly from Python fails.
Python Engine works as the MATLAB command "feval" does:
Most likely, calling "feval('api.Plex4.createExplicitSimplexStream')" in MATLAB will also fail. In such case, besides wrapping the Java API inside a M function, you may also invoke the Java API with eval in Python:
>>>eng.eval('api.Plex4.createExplicitSimplexStream', nargout=0)
  댓글 수: 1
Alexander Williams
Alexander Williams 2017년 1월 9일
eng.eval('api.Plex4.createExplicitSimplexStream', nargout=0)
results in
matlab.engine.MatlabExecutionError: Undefined variable "api" or class "api.Plex4.createExplicitSimplexStream".
I have decided to keep all calls to the java package in matlab code because of the issue mentioned in my comment above.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by