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.engineeng = matlab.engine.start_matlab()# prepare javaplex library for useeng.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):
DONEans =edu.stanford.math.plex4.streams.impl.ExplicitSimplexStream@5e894fecUndefined 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?
Best Answer