How is data passed from MATLAB to python?

조회 수: 15 (최근 30일)
Benjamin Avants
Benjamin Avants 2017년 11월 13일
답변: Pengfei Li 2018년 9월 11일
I have recently begun making python calls directly from MATLAB for a number of different things, including non-blocking .mat file saves. So far this has been working but I haven't been able to find documentation on the way data is being passed to the Python interpreter.
With Java, many sources indicate that data is passed by value and not by reference. This leads to a fair amount of overhead to transfer large amounts of data which I have noticed before in my applications. To deal with this I have designed my applications specifically to transfer data back and forth as little as possible between java and MATLAB and attempted to spread out that transfer as much as possible, so instead of transferring a large data structure all at once I transfer the data in chunks as it becomes available.
What I really want to know is whether or not I should be doing the same thing for Python. I have not yet noticed much overhead passing data to the Python interpreter, but I haven't tried passing anything particularly large yet (just a few MB at a time, so far). Can someone enlighten me about the specifics of the data passing functionality and best practices for dealing with it?

채택된 답변

Robert Snoeberger
Robert Snoeberger 2017년 11월 13일
All of the data conversions given in the following page of the documentation are passed by value (ie. data is copied from the MATLAB type to the Python type).
  댓글 수: 2
Benjamin Avants
Benjamin Avants 2017년 11월 13일
편집: Benjamin Avants 2017년 11월 13일
OK, so just to be sure I understand...
When the documentation says "MATLAB converts the data into types that best represent the data to the Python language" it is passing the data to Python by value, element by element.
It is not just creating a properly structured buffer or memory block and passing the reference.
This implies that transferring data to Python will take time that scales approximately linearly with the amount of data transferred and the transfer will block the MATLAB thread until all the data has been copied, correct?
Is the performance of this operation roughly the same as transferring data to java?
Robert Snoeberger
Robert Snoeberger 2017년 11월 14일
Yes, you are correct. Transferring data to Python will take time that scales linearly with the amount of data.

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

추가 답변 (1개)

Pengfei Li
Pengfei Li 2018년 9월 11일
hello,
I want to pass the matlab data to python to create a csc sparse matrix using python language in matlab. now I met a problem, can you help me? I know you are an expert in this way, because I look many of you answers. Thank you very much. my code in matlab as follow, I met the data type problem.
iK=[0 2 2 0 1 2];
jK=[0 0 1 2 2 2];
sK=[1 2 3 4 5 6];
data=py.numpy.array(sK);
ir=py.numpy.array(iK);
jr=py.numpy.array(jK);
A = py.scipy.sparse.csc_matrix(Data,ir,jr);
when I came to create the csc_matrix, then python would tell me the data type is not right. could you help me correct it? thank you very much.

카테고리

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