How can I access a matlab/octave module from python? How can I access a matlab/octave module from python? python python

How can I access a matlab/octave module from python?


You can use oct2py, which IIUC was started by its author because pytave didn't work on win32. It is successfully used in IPython through its octavemagic extension and I can tell it is easy to use on its own, the code is maintained (I reported a little Unicode bug and the author fixed it in a day) and works well. Most of the times is as simple as:

>>> from oct2py import octave>>> octave.run("cos(pi / 3)")'ans =  0.50000'>>> octave.call("foo", a, b)  # Function in foo.m

For further examples you can check this blog article.


Have you considered using OMPC, http://ompc.juricap.com/ ? I have used it with great success when not wishing to re-write some numerical linear algebra routines. I can imagine that the more esoteric the Matlab commands, the harder it would be to translate... but it might be worth a try. In the end, you're going to want to convert your Matlab code to Python because it will be a bottleneck on speed and performance. The only reason to leave the Matlab code in Matlab format is if it would be an enormous up-front cost to translate it all, which OMPC should mitigate somewhat. Otherwise, it's almost always worth that up-front cost to completely rid yourself of Matlab/Octave dependence.


I had some trouble getting OMPC to work because (I) the md5 module is deprecated, (II) Python 2.6 and later no longer accept arguments for Object.__new__() or Object.__init__(), and (III) the byteplay.py script needed to be updated.

To solve issue (I), I changed line 74 of yacc.py found in the ompc/ directory. This line imports md5 among other things. I deleted the md5 module and added the line below:

from hashlib import md5

Later in the yacc.py script, at line 1160, I changed,

Signature = md5.new()

to the following,

Signature = md5()

To run the code generated by ompcply.py, add 'from ompc import *' to the beginning of the file and then run it with an earlier version of Python, as:

$ python2.5 ompcply.py script.m > newscript.pym$ python2.5 newscript.pym

Using a version of Python later than 2.5 will give you the following error:

/home/connor/downloads/OMPC/ompclib/ompclib_numpy.py:66: DeprecationWarning: object.__new__() takes no parameters  a = super(mvar, cls).__new__(cls, *args, **kwargs)

To solve issue (III) I googled byteplay, and replaced the existing script with the newer version.