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

Discussion in 'Python' started by itcecsa, Dec 3, 2007.

  1. itcecsa

    itcecsa Guest

    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!
     
    itcecsa, Dec 3, 2007
    #1
    1. Advertising

  2. itcecsa

    markacy Guest

    On 3 Gru, 05:02, itcecsa <> 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!


    Why bother - use NumPy/SciPy/Matplotlib :)

    Cheers,
    Marek
     
    markacy, Dec 3, 2007
    #2
    1. Advertising

  3. itcecsa

    Guest

    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 <> 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!
     
    , Dec 3, 2007
    #3
  4. itcecsa

    Paul McGuire Guest

    On Dec 2, 10:02 pm, itcecsa <> 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!


    This comes up about once or twice a year, but I don't see the usual
    responders/suspects this time. A *while* back, I worked out how to
    run Matlab using the COM interface, and documented at this web page:
    http://www.geocities.com/ptmcg/python/using_matlab_from_python.html.
    I don't know if Matlab still supports the same interface, but this
    should get you started.

    -- Paul
     
    Paul McGuire, Dec 3, 2007
    #4
  5. itcecsa

    sturlamolden Guest

    On 3 Des, 05:02, itcecsa <> wrote:

    > 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!


    Do you really want to do that? NumPy, SciPy, Matplotlib, f2py, etc.
    are probably a better option. You don't need to rely on Matlab to do
    serious numerical and scientific computing in Python. Remember that
    NASA uses Python to process image data from the Hubble telescope.

    Anyhow... If you really need to do this, there are several ways to
    proceed:

    * Call the "Matlab engine" using ctypes. There is only nine C
    functions you need to wrap, and it should be straight forward. (This
    is what I would try first.)

    * Use Pyrex or SWIG to generate a wrapper to the Matlab engine C
    frontend instead of using ctypes.

    * Wrap the six functions in the Matlab engine's Fortran frontend with
    f2py.

    * Use pywin32 and COM to start Matlab as an ActiveX server
    ("Matlab.Application").

    * Spawn a Matlab process using the subprocess submodule. Communicate
    with your Matlab script's standard input and output streams (file id 1
    in Matlab).

    * See if you can get the old PyMat wrapper for the Matlab engine to
    build and work.
     
    sturlamolden, Dec 4, 2007
    #5
  6. itcecsa

    Guest

    On Dec 3, 6:12 pm, sturlamolden <> wrote:
    > On 3 Des, 05:02, itcecsa <> wrote:
    >
    > > 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!


    http://mlabwrap.sourceforge.net is under
    active development -- Phil
     
    , Dec 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. siki
    Replies:
    0
    Views:
    961
  2. toch3

    CANNOT RUN!!!!! any idea why?

    toch3, Aug 27, 2006, in forum: C Programming
    Replies:
    1
    Views:
    300
    Gordon Burditt
    Aug 27, 2006
  3. Dr Mephesto

    App idea, Any idea on implementation?

    Dr Mephesto, Feb 4, 2008, in forum: Python
    Replies:
    3
    Views:
    720
    Dennis Lee Bieber
    Feb 5, 2008
  4. Replies:
    0
    Views:
    635
  5. Luna Moon
    Replies:
    16
    Views:
    1,339
    Giovanni Gherdovich
    Aug 8, 2008
Loading...

Share This Page