1

Following the answer in How to efficiently convert Matlab engine arrays to numpy ndarray?, it seems much more efficient to access the matlab engine array through the _data property. However it appears that there is no _data property when the array returned by Matlab is a 'complex single' one. Is there an equivalent fast access to the array of complex numbers ?

CDJB
  • 14,043
  • 5
  • 29
  • 55
Greg
  • 11
  • 1
  • 1
    What version of MATLAB are you using? Prior to R2018a, complex data was stored as two separate memory blocks, one for the real component and one for the imaginary component. Since R2018a, it is stored interleaved in a single memory block. I suspect your answer will be different depending on the internal storage. – Cris Luengo Jan 30 '20 at 17:17
  • I am using R2016b. More exactly I am interfacing Python with a packaged version of Matlab code using the MCR 9.1. I am novice to Python and the Python-Matlab interface and I don't know how to see what members can be accessed from a Matlab object such as this _data. – Greg Jan 31 '20 at 08:22

1 Answers1

0

A possible workaround is to return from Matlab two real arrays (one containing the real part, the other the imaginary part) and build back the complex value in Python

M_real, M_imag = myMatlabFunction()
M_real_np = np.array(M_real._data)
M_imag_np = np.array(M_imag._data)
M_np = M_real_np + M_imag_np*np.complex(0,1)

Then we can profit from the fast access to the _data member of each array.

I am still interested in more straightforward solution.

Greg
  • 11
  • 1