Call MATLAB.m files and functions in a Python script

I have a platform for python scripts and I would like to call matlab functions inside. I found several topics dealing with the problem, among which these two

How do I interact with MATLAB with Python?

Running m files from Python

However, the streams are either not the last or not very detailed.

  • is mlabwrap reliable?
  • what would you act as a solution to calling matlab / .m file functions in a python script?
  • Using win32com from python to call a matlab session -> is this a good idea? could you point out more documents or examples on this topic?

Looks like the link to sourceForge is not updated, last update 2010,

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

  1. Could you hint at some latest version?

thanks

+6
source share
3 answers

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

  • mlabwrap will work reliably on different platforms, in versions of Python and Matlab. However, it has limitations, and it will fail reliably if these limits are exceeded. You can usually get around them.
  • See my answer here for more information on calling Matlab functions and Matlab scripts via mlabwrap. This answer also describes how to get around one of the main limitations of mlabwrap, i.e. not all Matlab objects can be directly converted to Python objects.
  • I don't know anything about calling Matlab using win32com.

I used mlabwrap in what I will call the "Python-primary style", where most Python programs, using Matlab as a library for certain mathematical functions that are not available in scipy / numpy, and in "Matlab-primary", most programs are in Matlab, and the final results are imported into Python for use in some external process.

For Python-primary, it's important to keep in mind that not all Matlab functions return Python-readable data. mlabwrap will return the MLabObjectProxy object from these functions. This usually happens when you use Matlab functions to create objects that are passed to other Matlab functions to actually process the data. For example, you can use the digital signal processing toolbar 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 go past them, the more likely you are to find a bug in mlabwrap. Instead, you can write a simple Matlab wrapper function that receives an object, processes the data, and then returns the corresponding result as an array.

You can also work around issues with MLabObjectProxies by using the low level commands in mlabwrap. For example, if I have matlab_struct , which is a struct array with a matlab_struct.label field, and I only want 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 lower-level commands are available: mlab._set('variable_name', variable) , mlab.eval('command string') and mlab.get('variable_name') .

If I do a lot of processing in Matlab, say in a toolbar or plugin that is not available elsewhere, I will 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, invoking .m scripts, saving the result in a data file, and importing it into my Python code.

Good luck

+7
source

mlabwrapper

- great solution for python ↔ MATLAB bridge. If something doesn’t work, you are just reporting specific issues on SO :)

You should notice that mlabwrapper as a project has been around for quite some time. http://mlabwrap.sourceforge.net/

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

The file associated with this is a copy of mlabwrap v1.1-pre ( http://mlabwrap.sourceforge.net/ ), as described here: http://sourceforge.net/mailarchive/message.php?msg_id=27312822

with the bug fix:

mlabraw.cpp: 225: error: incorrect conversion from 'const mwSize * to' Const int * Also note that in Ubuntu you need sudo apt-get install CSH

See http://github.com/aweinstein/mlabwrap for details

MLab

After spending more time, I made a github mirror for updating, fixing bugs, and wrapper support https://github.com/ewiger/mlab (patches and pull requests are welcome!)

A protocol can be set, i.e.

 pip install mlab 

I excluded the cpp implementation for now. In the current mode, it works as follows:

A connection to the MATLAB instance is created for the Linux / Mac library. The rest is serialization (partially denoted by @brentlance), which is done using numpy.

For the Windows library, DCOM is used for communication. (But I still fix the version search using the registry).

When to use mlab?

I would recommend calling very high user-defined functions in MATLAB (mostly returning boolean results or very standard built-in types as matrices) to minimize any association with MATLAB. This approach is ideal for legacy code, but some wrapper interfaces may need to be written to simplify function declarations.

In general, the code is a bit cumbersome and multifaceted. The main part (now matlabpipe and matlabcom ) seems to do a good job of this. Ultimately, I would not recommend mlab for a full-blown, productive application unless you want to test time, report bugs, fix bugs, and request all your use cases.

+3
source

To answer your questions:

  • We used mlabwrap and it was reliable. The only difficulty was compiling it. We had a few problems with this. However, once you compile it, it works well.
  • I would recommend matlab_wrapper because it is much easier to install (pure Python, without compilation), supports various data structures (numeric, logical, structural, cell arrays) and runs on GNU / Linux, Windows, OSX.
  • Using win32com to communicate with MATLAB should work, but it is rather low-level and not portable.

Disclaimer: I am the author of matlab_wrapper.

+1
source

All Articles