필터 지우기
필터 지우기

Python is not loaded for an engine mode MATLAB session

조회 수: 2 (최근 30일)
Ilya Tyuryukanov
Ilya Tyuryukanov 2016년 6월 28일
댓글: Ilya Tyuryukanov 2016년 6월 29일
I'm starting MATLAB from a Python script by using the following commands:
import matlab.engine
eng = matlab.engine.start_matlab() # get MATLAB
eng.desktop(nargout=0) # interactive MATLAB session
However, the started MATLAB session doesn't recognize Python commands. For example, pyversion returns
version: ''
executable: ''
library: ''
home: ''
isloaded: 0
When MATLAB is started 'normally' (e.g. via its desktop shortcut), pyversion gives a meaningful output and it is possible to run Pythin commands in MATLAB.
The motivation to use Python in the MATLAB session started from Python is to convert the MATLAB output results to Python datatypes via py.list and similar commands. I.e. I don't want to get something like matlab.double back to Python.
While it is possible to circumvent this issue by smartly converting the MATLAB output to cells and then merging the resulting lists in Python, it would be interesting to clarify why Python is not accessible inside MATLAB in this particular case.
I'm trying to pass an n-by-2 matrix from MATLAB as a list of 2-tuples to Python (i.e. each matrix row should become a 2-tuple). Currently I'm doing the following:
col1 = num2cell(uint32(matr(:, 1)), 2);
col2 = num2cell(uint32(matr(:, 2)), 2);
And then in Python (cols is the output of the MATLAB function retrieved in Python):
col1 = cols[0]
col2 = cols[1]
lst = list(zip(col1, col2 ))
  댓글 수: 2
Robert Snoeberger
Robert Snoeberger 2016년 6월 28일
"The motivation to use Python in the MATLAB session started from Python is to convert the MATLAB output results to Python datatypes via py.list and similar commands. I.e. I don't want to get something like matlab.double back to Python."
This will not work the way you are expecting. If you look at the table "MATLAB Scalar Type to Python Type Mapping" [1], py.list will behave according to the row "MATLAB handle object," which means that you will get back a matlab.object.
Ilya Tyuryukanov
Ilya Tyuryukanov 2016년 6월 28일
Thank you very much for this comment, then I'll resort to the second solution that I've mentioned, i.e. to convert the output to cell arrays and assemble them back in Python. I've updated the question to show what I'm currently doing.

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

채택된 답변

Robert Snoeberger
Robert Snoeberger 2016년 6월 28일
Does this help?
import sys
eng.pyversion(sys.executable, nargout=0)
  댓글 수: 1
Ilya Tyuryukanov
Ilya Tyuryukanov 2016년 6월 29일
eng.pyversion('C:\\Python34\\pythonw.exe', nargout=0) has helped indeed.

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

추가 답변 (0개)

카테고리

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