Running MATLAB engine from MATLAB

3 views (last 30 days)
Adrian
Adrian on 17 Oct 2018
Commented: Hadi Salimi on 11 Mar 2021
Currently I work on a project where I have to use the MATLAB engine in Python. This Python module I want to use in MATLAB. I.e. I start a Python module in MATLAB which starts the MATLAB engine and calls a function in MATLAB which return value then is processed in my Python module. The return value from the Python module then should be further processed in MATLAB.
I get this error message:
Error using enginesession>__init__ (line 8)
Python Error: EngineError: MATLAB process session cannot be initialized.
Error in __init__><module> (line 82)
_session = EngineSession()
Error in __init__>import_module (line 37)
__import__(name)
I get a similar error if I try to load the MATLAB engine from MATLAB by using
py.importlib.import_module('matlab.engine')
Is there a way to solve my issue or is this just simply not possible with the MATLAB engine?
  4 Comments
Adrian
Adrian on 19 Oct 2018
Edited: Adrian on 19 Oct 2018
"""
Loads parameters from mat-files and checks if parameters are missing or not.
If parameters are missing they get default values.
Note: matlab.engine should be imported before ANNarchy as they interfere
with each other.
"""
import matlab.engine
import globalParams
from ANNarchy import *
# Initialization
expType = globalParams.expType # get type of experiment
#expType = 'SSDtester'
eng = matlab.engine.start_matlab()
showDisp = False
pathSSD = 'mat/original/'
pathStim = 'mat/Stim/'
def loadParam():
# for SSDtester
if expType == "SSDtester":
tmp1 = eng.mat2py(pathSSD+'main.mat','parameter')
d = tmp1['parameter']
tmp2 = eng.mat2py(pathSSD+'test.mat','parameter')
tmp2 = tmp2['parameter']
tmp3 = eng.mat2py(pathSSD+'ssd.mat', 'parameter')
tmp3 = tmp3['parameter']
d.update(tmp2)
d.update(tmp3)
...
mat2py
function d = mat2py(file, data)
%MAT2PY Return a MATLAB variable to pass it to Python
% File should be a .mat-file and data a variable/structure/array in the
% file
d = load(file, data);
end
I use Python 3.6. I get an error when importing the modules. The error occurs when I run the module in MATLAB via the API and matlab.engine is imported, to be precise. If I run the module from the Python shell it works without errors.
Hadi Salimi
Hadi Salimi on 11 Mar 2021
Did you find any soloutions regarding your issue?

Sign in to comment.

Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!