A Tool to Convert Matlab Code to Python

A tool to convert MATLAB code to Python

There are several tools for converting Matlab to Python code.

The only one that's seen recent activity (last commit from June 2018) is Small Matlab to Python compiler (also developed here: SMOP@chiselapp).

Other options include:

  • LiberMate: translate from Matlab to Python and SciPy (Requires Python 2, last update 4 years ago).
  • OMPC: Matlab to Python (a bit outdated).
  • Mat2py: Matlab to Python (Requires Python 2).

Also, for those interested in an interface between the two languages and not conversion:

  • pymatlab: communicate from Python by sending data to the MATLAB workspace, operating on them with scripts and pulling back the resulting data.
  • Python-Matlab wormholes: both directions of interaction supported.
  • Python-Matlab bridge: use Matlab from within Python, offers matlab_magic for iPython, to execute normal matlab code from within ipython.
  • PyMat: Control Matlab session from Python.
  • pymat2: continuation of the seemingly abandoned PyMat.
  • mlabwrap, mlabwrap-purepy: make Matlab look like Python library (based on PyMat).
  • oct2py (repository): run GNU Octave commands from within Python.
  • pymex: Embeds the Python Interpreter in Matlab, also on File Exchange.
  • matpy: Access MATLAB in various ways: create variables, access .mat files, direct interface to MATLAB engine (requires MATLAB be installed).
  • MatPy: Python package for numerical linear algebra and plotting with a MatLab-like interface.

Btw might be helpful to look here for other migration tips:

  • http://bci2000.org/downloads/BCPy2000/Migration.html

On a different note, for people who might find it useful there is:

  • matlab2fortran

How to convert matlab code to python code automatically

Unfortunately, no such tool exists, especially for complex things like conversion of toolboxes.

I suggest that you try to find places in your codebase where you could divide your logic into self-contained modules. Then, you can work on converting your code one module at a time. You should already start to familiarize with numpy which will serve as the replacement for the majority of your matrix math operations. Then you will need to find ways to patch together your code using tools like the MATLAB engine.

It's worth noting that converting Python 2 to 3 is relatively straightforward and there is an official tool (2to3) that can help you do this automatically. However, be careful with converter like the one you found, a quick scan of their github shows 65 outstanding issues and no updates for nearly 3 years.

Good luck with the conversion, although it will not be as easy as you may have hoped, it will be worth in in the long run.

data type not understood when converting Matlab code to Python

data = [v; zeros(nplots,N)] this is concatenating two matrices and stacken them up, note the ; in numpy you can use numpy.concatenate((v, zeros((nplots,N))), axis = 0) where axis is by which axis you want to concatenate by ...

data = np.array(v,zeros(nplots,N));
TypeError: data type not understood

basically when you call np.array the fist argument must be iterable object, list, tuple and on the second argument must be the type ie 'int', 'float32', 'float32' and so on ... but you set the type to zeros(nplots,N) numpy is complaining that it isn't a type ...
numpy.zeros is the same the first argument must be a tuple and the second the type, sorry about that I didn't properly include ()

it should be data = numpy.concatenate((v, numpy.zeros((nplots,N))), axis = 0) assuming that you want to use double type which is the standard type.



Related Topics



Leave a reply



Submit