Interface numpy arrays to Matlab?
![](https://secure.gravatar.com/avatar/0b7d465c9e16b93623fd6926775b91eb.jpg?s=120&d=mm&r=g)
I've searched but haven't found any decent answer. I need to call Matlab from python. Matlab has a python module for this purpose, but it doesn't understand numpy AFAICT. What solutions are there for efficiently interfacing numpy arrays to Matlab? Thanks, Neal
![](https://secure.gravatar.com/avatar/93a76a800ef6c5919baa8ba91120ee98.jpg?s=120&d=mm&r=g)
If you can use Octave instead of Matlab, I've had a very good experience with Oct2Py: https://github.com/blink1073/oct2py On Mon, Aug 28, 2017 at 12:20 PM, Neal Becker <ndbecker2@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/f9edf141570be78631565a8c8883bde5.jpg?s=120&d=mm&r=g)
Looks like Transplant can handle this use-case. Blog post: http://bastibe.de/2015-11-03-matlab-engine-performance.html GitHub link: https://github.com/bastibe/transplant I haven't given it a try myself, but it looks promising. On Mon, Aug 28, 2017 at 4:21 PM, Stephan Hoyer <shoyer@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/d4e1f9c950b7fd5858cb9f0b7c7c85fd.jpg?s=120&d=mm&r=g)
I have not used Transplant, but it sounds fairly similar to Python-matlab-bridge. We currently optionally call Matlab via Python-matlab-bridge in some of the the tests for the PyWavelets package. https://arokem.github.io/python-matlab-bridge/ https://github.com/arokem/python-matlab-bridge I would be interested in hearing about the benefits/drawbacks relative to Transplant if there is anyone who has used both. On Mon, Aug 28, 2017 at 4:29 PM, CJ Carey <perimosocordiae@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/0b7d465c9e16b93623fd6926775b91eb.jpg?s=120&d=mm&r=g)
Transplant sounds interesting, I think I could use this. I don't understand though why nobody has used a more direct approach? Matlab has their python API https://www.mathworks.com/help/matlab/matlab-engine-for-python.html. This will pass Matlab arrays to/from python as some kind of opaque blob. I would guess that inside every Matlab array is a numpy array crying to be freed - in both cases an array is a block of memory together with shape and stride information. So I would hope a direct conversion could be done, at least via C API if not directly with python numpy API. But it seems nobody has done this, so maybe it's not that simple? On Mon, Aug 28, 2017 at 5:32 PM Gregory Lee <grlee77@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/03f2d50ce2e8d713af6058d2aeafab74.jpg?s=120&d=mm&r=g)
On Tue, Aug 29, 2017 at 1:08 PM, Neal Becker <ndbecker2@gmail.com> wrote:
I was going to suggest this Stack Overflow post earlier but figured that you must have found it already: https://stackoverflow.com/questions/34155829/how-to-efficiently-convert-matl... Based on that it seems that at least arrays returned from the MATLAB engine can be reasonably converted using their underlying data (`_data` attribute, together with the `size` attribute to unravel multidimensional arrays). The other way around (i.e. passing numpy arrays to the MATLAB engine) seems less straightforward: all I could find was https://www.mathworks.com/matlabcentral/answers/216498-passing-numpy-ndarray... The comments there suggest that you can instantiate `matlab.double` objects from lists that you can pass to the MATLAB engine. Explicitly converting your arrays to lists along this step don't sound too good to me. Disclaimer: I haven't tried either methods. Regards, András Deák
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
On Tue, Aug 29, 2017 at 4:08 AM, Neal Becker <ndbecker2@gmail.com> wrote:
I agree -- it is absolutley bizare that they havn'etr built in a numpy array <-> matlab array mapping! MAybe they do'nt want Matlb usres to realize that nmpy provides most of what MATLAB does (but better :-) ) -- and want people to use Python with MATlab for other pytonic stuff that MATLAB doesn't do well.... but they do provide a mapping for array.array: https://www.mathworks.com/help/matlab/matlab_external/use-python-array-array... which is a buffer you can wrap a numpy array around efficiently.... odd that you'd have to write that code. -CHB
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
![](https://secure.gravatar.com/avatar/93a76a800ef6c5919baa8ba91120ee98.jpg?s=120&d=mm&r=g)
If you can use Octave instead of Matlab, I've had a very good experience with Oct2Py: https://github.com/blink1073/oct2py On Mon, Aug 28, 2017 at 12:20 PM, Neal Becker <ndbecker2@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/f9edf141570be78631565a8c8883bde5.jpg?s=120&d=mm&r=g)
Looks like Transplant can handle this use-case. Blog post: http://bastibe.de/2015-11-03-matlab-engine-performance.html GitHub link: https://github.com/bastibe/transplant I haven't given it a try myself, but it looks promising. On Mon, Aug 28, 2017 at 4:21 PM, Stephan Hoyer <shoyer@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/d4e1f9c950b7fd5858cb9f0b7c7c85fd.jpg?s=120&d=mm&r=g)
I have not used Transplant, but it sounds fairly similar to Python-matlab-bridge. We currently optionally call Matlab via Python-matlab-bridge in some of the the tests for the PyWavelets package. https://arokem.github.io/python-matlab-bridge/ https://github.com/arokem/python-matlab-bridge I would be interested in hearing about the benefits/drawbacks relative to Transplant if there is anyone who has used both. On Mon, Aug 28, 2017 at 4:29 PM, CJ Carey <perimosocordiae@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/0b7d465c9e16b93623fd6926775b91eb.jpg?s=120&d=mm&r=g)
Transplant sounds interesting, I think I could use this. I don't understand though why nobody has used a more direct approach? Matlab has their python API https://www.mathworks.com/help/matlab/matlab-engine-for-python.html. This will pass Matlab arrays to/from python as some kind of opaque blob. I would guess that inside every Matlab array is a numpy array crying to be freed - in both cases an array is a block of memory together with shape and stride information. So I would hope a direct conversion could be done, at least via C API if not directly with python numpy API. But it seems nobody has done this, so maybe it's not that simple? On Mon, Aug 28, 2017 at 5:32 PM Gregory Lee <grlee77@gmail.com> wrote:
![](https://secure.gravatar.com/avatar/03f2d50ce2e8d713af6058d2aeafab74.jpg?s=120&d=mm&r=g)
On Tue, Aug 29, 2017 at 1:08 PM, Neal Becker <ndbecker2@gmail.com> wrote:
I was going to suggest this Stack Overflow post earlier but figured that you must have found it already: https://stackoverflow.com/questions/34155829/how-to-efficiently-convert-matl... Based on that it seems that at least arrays returned from the MATLAB engine can be reasonably converted using their underlying data (`_data` attribute, together with the `size` attribute to unravel multidimensional arrays). The other way around (i.e. passing numpy arrays to the MATLAB engine) seems less straightforward: all I could find was https://www.mathworks.com/matlabcentral/answers/216498-passing-numpy-ndarray... The comments there suggest that you can instantiate `matlab.double` objects from lists that you can pass to the MATLAB engine. Explicitly converting your arrays to lists along this step don't sound too good to me. Disclaimer: I haven't tried either methods. Regards, András Deák
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
On Tue, Aug 29, 2017 at 4:08 AM, Neal Becker <ndbecker2@gmail.com> wrote:
I agree -- it is absolutley bizare that they havn'etr built in a numpy array <-> matlab array mapping! MAybe they do'nt want Matlb usres to realize that nmpy provides most of what MATLAB does (but better :-) ) -- and want people to use Python with MATlab for other pytonic stuff that MATLAB doesn't do well.... but they do provide a mapping for array.array: https://www.mathworks.com/help/matlab/matlab_external/use-python-array-array... which is a buffer you can wrap a numpy array around efficiently.... odd that you'd have to write that code. -CHB
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (6)
-
Andras Deak
-
Chris Barker
-
CJ Carey
-
Gregory Lee
-
Neal Becker
-
Stephan Hoyer