Calling MATLAB .m-files and functions in Python script
Asked Answered
C

4

9

I have a platform for python scripting, and I would like to call matlab functions inside. I have found several threads tackling the issue, among them those two

How do I interact with MATLAB from Python?

Running m-files from Python

However, threads are either not recent, or not very detailed.

  1. is mlabwrap reliable?
  2. what would you advocate as a solution to call matlab functions / .m files in python script?
  3. using win32com from python to call a matlab session --> is this a good idea ? could you point to more doc or examples on this topic?

looks like link to sourceForge is not up to date, last update 2010,

http://sourceforge.net/projects/mlabwrap/

  1. Could you hint at some latest version ?

Thanks

Catchpole answered 19/11, 2012 at 16:12 Comment(3)
Though mlabwrap has not been updated for a while, there are several very recent and very positive comments. As such I guess the recommendation still stands.Hemimorphite
@FredrikRedin nothing: i dont have matlab on my workstation. its a general question.Catchpole
Looks like a partial duplicate of #2883689Dissuasive
P
7

I would still recommend mlabwrap as the solution for this. I use mlabwrap on a regular (weekly?) basis, on both Linux and Windows, across several different versions of Python, and several different versions of Matlab. To answer your specific questions:

  1. mlabwrap will reliably perform across platforms, Python, and Matlab versions. It does however have limitations, and it will reliably fail when pushed past those limitations. Usually, these can be worked around.
  2. See my answer here for more information on calling Matlab functions vs. Matlab scripts through mlabwrap. This answer also describes how to workaround one of the primary limitations of mlabwrap, which is that not all Matlab objects can be directly converted into Python objects.
  3. I don't know anything about calling Matlab using win32com.

I have used mlabwrap in what I'll term 'Python-primary' style, where the majority of the programming in Python, using Matlab as a library for specific mathematical functions that aren't available in scipy/numpy, and in a 'Matlab-primary' style, the majority of the programming is in Matlab, and the final results are importedinto Python for use in some external process.

For Python-primary, the thing to keep in mind is that not all Matlab functions will return Python-readable data. mlabwrap will return a MLabObjectProxy object from these functions. These commonly occur when you use Matlab functions to create objects that are passed into other Matlab functions to actually process the data. For example, you can use the digital signal processing toolbox to create a Welch spectrum object which you can then use to get the power spectrum of your data. Theoretically, you can pass these MLabObjectProxies into Matlab functions that require them. In my experience the more you pass these back and forth, the more likely you are to find a bug in mlabwrap. What you can do instead is write a simple Matlab wrapper function obtains the object, processes the data, and then returns appropriate output as an array.

You can also get around problems with the MLabObjectProxies by using the low-level commands in mlabwrap. For example, if I have a matlab_struct that is a struct array with field matlab_struct.label, and I only want the labels on the Python side, I can do the following:

# place matlab_struct into the Matlab workspace 
mlab._set('matlab_struct', matlab_struct) 
# convert the labels into a cell array
matlab_struct_labels = mlab.eval('{matlab_struct.labels}')

The main low-level commands available are mlab._set('variable_name', variable), mlab.eval('command string'), and mlab.get('variable_name').

If I'm doing a lot of heavy-duty processing in Matlab, say in a toolbox or plugin that isn't available elsewhere, I'll write what I call 'Matlab-primary' code, where I try to avoid passing data back and forth through mlabwrap, instead manipulating variables in the Matlab workspace by calling .m scripts, saving the resulting output to a data file, and importing that into my Python code.

Good luck!

Pontificals answered 19/11, 2012 at 18:58 Comment(0)
D
3

mlabwrapper

is a great solution for python <-> MATLAB bridging. If something is not working for you just report concrete problems on SO :)

You have to note that mlabwrapper as project has been around for quite a while. http://mlabwrap.sourceforge.net/

I had problems with mlabwrap.cpp recently, for which I found the following github fork

Related Thereis is a copy of mlabwrap v1.1-pre (http://mlabwrap.sourceforge.net/) patched as described here: http://sourceforge.net/mailarchive/message.php?msg_id=27312822

with a patch fixing the error:

mlabraw.cpp:225: error: invalid conversion from ‘const mwSize*’ to ‘const int*’ Also note that in Ubuntu you need to sudo apt-get install csh

For details see http://github.com/aweinstein/mlabwrap

mlab

After spending more time, I made a github mirror to update, bugfix and maintain the wrapper https://github.com/ewiger/mlab (patches and pull-requests are welcomed!)

It can be pip installed, i.e.

pip install mlab

I have excluded the cpp implementation for now. In the current way it works as the following:

For Linux/Mac library creates a pipe connection with MATLAB instance. The rest is serialization (partially pointed out by @brentlance), which is done using numpy.

For Windows library uses DCOM to communicate. (But I am still to fix version lookup using registry).

When to use mlab?

I would recommend to call very high level user-functions in MATLAB (mostly returning logical results or very standard built-in types as matrices) to minimize any communication with MATLAB. This approach is perfect for legacy code, but might require writing some wrapping interfaces to simplify function declarations.

Overall, the code is a bit cumbersome and is a patchwork of many. The core part (now this is matlabpipe and matlabcom) seems to do the job pretty well. Ultimately, I would not recommend mlab for full-scale productive application unless you are willing to spend time testing, bug-reporting, bug-fixing and feature-requesting all of your use-cases.

Dissuasive answered 18/8, 2013 at 17:13 Comment(0)
C
1

To answer your questions:

  1. We used mlabwrap and it was reliable. The only difficulty was to compile it. We had a few issues with that. However, once you compile it, it runs well.
  2. I would recommend matlab_wrapper, because it's much easier to install (pure Python, no compilation), supports various data structures (numeric, logical, struct, cell arrays) and runs on GNU/Linux, Windows, OSX.
  3. Using win32com to communicate with MATLAB should work, but it's quite low-level and not portable.

Disclaimer: I'm the author of matlab_wrapper.

Constrict answered 20/7, 2014 at 13:59 Comment(0)
L
0

Since MATLAB R2014b, there is now a MATLAB API for Python. See the MATLAB documentation for more information. It however requires to start the MATLAB engine which can take some time.

Lurcher answered 9/8, 2018 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.