Any idea how to open Matlab and run M-files by using Python?

DelDotDr at gmail.com DelDotDr at gmail.com
Mon Dec 3 05:36:44 EST 2007


Hello,

One solution I can think of would be to write an interface to the
Matlab command prompt using pexpect, the python implementation of
expect. Expect allows you to spawn and interact with other process.
There is already a cool implementation of exactly this in Sage, an
open source computer algebra system written in python.

If you want to see something work immediately, try installing sage
(sage.math.washington.edu/sage).

I did some experimenting (in a linux environment) and it works quite
nicely.

Here is an example:

I wrote a simple matlab function myAdd saved as myAdd.m

function thesum = myAdd(a, b)
thesum = a+b;

I put the file in my Matlab search path.

In sage, I did this:
sage: a = matlab('myAdd(2,2)')
sage: a
      4

This would require you to run your python scripts using Sage instead
of just python.
You can import the sage library into python, but it's kind of non-
trivial.

At the very least, you can study sage's matlab interface, which is
written in python. It is very easy to spawn process and give commands
with pexpect, but not as fun to get results because it involves
parsing to get the output.

Good luck,
-Dorian

On Dec 2, 8:02 pm, itcecsa <itce... at gmail.com> wrote:
> Hi,
>
> I am implementing a small Python project, what I am going to do is to
> open Matlab and run some M-files, and get some output from Matlab
> command prompt.
>
> I have no idea how to open Matlab from Python!
>
> Any suggestions would be appriciated!




More information about the Python-list mailing list