473,388 Members | 948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 13704
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.