How to pass a structure from Matlab to Python

조회 수: 9 (최근 30일)
Jason Shih
Jason Shih 2015년 7월 3일
답변: Jason Shih 2015년 7월 8일
I'm using Matlab(2014b) and Python(2.7.6) for mix programming. I tried to pass a structure from Matlab to Python,but I failed.
The structure I want to pass is:
dmodel = struct('regr',regr, 'corr',corr, 'theta',theta.', ...
'beta',fit.beta, 'gamma',fit.gamma, 'sigma2',sY.^2.*fit.sigma2, ...
'S',S, 'Ssc',[mS; sS], 'Ysc',[mY; sY], ...
'C',fit.C, 'Ft',fit.Ft, 'G',fit.G);
In the structure all the elements are scalar except Ssc and Ysc.
The Matlab script looks like this:
function model=Fun_mat()
...
some command;
...
model = dmodel;
end
and the Python script looks like this:
import matlab.engine as meng
eng = meng.start_matlab()
dmodel = eng.Fun_mat()
eng.quit()
These two scripts are in the same directory.
What should I do? Any help is appreciate!

채택된 답변

Gareth Thomas
Gareth Thomas 2015년 7월 5일
Hi Jason,
Have you seen this page in the R2015a documentation:
Please note that this is used for the MATLAB engine which can be called from python. I hope that this helps you.
Best regards, Gareth
  댓글 수: 1
Jason Shih
Jason Shih 2015년 7월 6일
편집: Jason Shih 2015년 7월 6일
Hi Gareth,
Thanks for your reply. I have read that document before, but I still have some problems.
The structure I want is
dmodel =
regr: 1
corr: 2
theta: [0.4419 0.1091]
beta: [6x1 double]
gamma: [1x100 double]
sigma2: 949.8115
S: [100x2 double]
Ssc: [2x2 double]
Ysc: [2x1 double]
C: [100x100 double]
Ft: [100x6 double]
G: [6x6 double]
(I'm sorry I was wrong about the element type before.)
The problem is that when Matlab return the struture with element 'C'(a 100*100 double matrix), I get the messeage below in Python:
Segmentation fault (core dumped)
But when the Matlab return the structre without the element 'C', I get no error in Python.
What should I do?
Best regards, Jason

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

추가 답변 (1개)

Jason Shih
Jason Shih 2015년 7월 8일
I have figured out what's wrong in that script. The element 'C' in the structure in a sparse matrix, which in not supported by the engine now.

카테고리

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