How to have matlab engine return value of variable in python

조회 수: 17 (최근 30일)
Amine Aboufirass
Amine Aboufirass 2017년 6월 4일
댓글: i621148 2019년 9월 20일
I currently use the following set of commands to load up a Matlab session in Python:
import matlab.engine
eng = matlab.engine.start_matlab("-desktop")
I have become familiar with a few commands using this engine such as
eng.eval()
eng.cd()
eng.load()
Following the same pattern, if I have a variable in matlab named:
VariableName
I should like to call this variable directly using the engine and have its value return in a python compatible format. However, neither
eng.VariableName
nor
eng.VariableName()
works. For the first attempt I get the following output in my matlab session :
<matlab.engine.matlabengine.MatlabFunc at 0x113dde80>
Which is not very helpful.
Also, if I have a user-defined function already added to the matlab file path using addpath(), I should also like to call this function from python. If the function FunctionName can take in variable VariableName, then my intuition says that something like this:
eng.FunctionName(eng.VariableName)
should work. However it does not.
How can I 1) return the values of variables in my Python session directly from matlab (without using eng.eval) and 2) call user defined matlab functions and pass in user defined variables (again without utilizing eng.eval)??

답변 (1개)

Robert Snoeberger
Robert Snoeberger 2017년 6월 5일
1) Use eng.workspace['VariableName']. See here.
  댓글 수: 1
i621148
i621148 2019년 9월 20일
If using Spyer, there is an option in the Variable Explorer called Exclude unsupported types
that if unchecked will show all variable types on it.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by