473,503 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 13713
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1828
by: lmo863 | last post by:
Hi, I was wondering if any of you know what an easier way to turn matlab code into c++. I have had a look on the internet and Mathtools.com used to do a program called matcom, but it no longer...
7
4203
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
3
9872
by: Fabian Ruranski | last post by:
hi, i'm searching for a way to connect matlab from c#. im vb it is very easy: Set MatLab = CreateObject("Matlab.Application") result = MatLab.execute("a=5")
6
22232
by: Lars Christiansen | last post by:
Hi! I am a master student in (geo)physics at the University of Copenhagen and part of a study group on C++ as a scientific programming language. I, and the other students in the group, have...
1
1263
by: dd | last post by:
Hi, I'm porting some matlab programs to C and wonder wether someone has tried to use C to realize some data structure like cells in Matlab? I used ***p to realize cells that are like k*m*n,...
2
12522
by: Binod Pant | last post by:
Does any one know of a way to call MATLAB from PHP/Perl? I found the Perl module Math:MATLAB , but it seems not to work on Windows, and it's not easy to install and use. This would enable one to...
1
2120
by: abhiM | last post by:
Is there a way to use the MFC Application wizard from VC++ 2003 to assign area for Matlab plots(say in a dialog, as you would for a text box) and then generate the Matlab plots in that area? ,...
4
3408
by: renaysingh | last post by:
Hi I am new to C programming. What is the best way to get Matlab's any and all functions working in C? Is there a library available? Which is the best for speed? For reference the...
2
5293
by: zipls | last post by:
suppose a simple matlab function //add.m function result=add(left,right) result=left + right; //end of add.m use matlab comtool to encapsulate the function in fun.dll and then use it in...
14
3116
by: Luna Moon | last post by:
Dear all, Can C++/STL/Boost do the vectorized calculation as those in Matlab? For example, in the following code, what I really want to do is to send in a vector of u's. All other...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.