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

I

itcecsa

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

markacy

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
 
D

DelDotDr

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
 
P

Paul McGuire

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
 
S

sturlamolden

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top