By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 3 Gru, 05:02, itcecsa <itce...@gmail.comwrote:
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
Dec 3 '07 #2

P: n/a
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...@gmail.comwrote:
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 '07 #3

P: n/a
On 3 Des, 05:02, itcecsa <itce...@gmail.comwrote:
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.

Dec 4 '07 #4

P: n/a
On Dec 3, 6:12 pm, sturlamolden <sturlamol...@yahoo.nowrote:
On 3 Des, 05:02, itcecsa <itce...@gmail.comwrote:
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 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.