Am 08.06.2011 07:12 schrieb nazmul.is...@gmail.com:
I need to call a python function from a Matlab environment. Is it
possible?

Let's assume, I have the following python code:

def squared(x):
     y = x * x
     return y

I want to call squared(3) from Matlab workspace/code and get 9.

Thanks for your feedback.

Nazmul

You can write a .mex file which calls Python API functions. These should be, among others,

init:
Py_Initialize()

for providing callbacks or so:
Py_InitModule()

for running:
PyRun_String()
PyRun_SimpleString()
PyImport_Import()
  -> PyObject_GetAttrString()
  -> PyCallable_Check()
  -> PyObject_CallObject()
etc. etc.

at the end:
Py_Finalize()


For details, have a closer look at the API documentation, e. g. http://docs.python.org/c-api/veryhigh.html.

Additionally, you have to care about conversion of data types between the MATLAB and Python types. Here you can look at

http://www.mathworks.com/help/techdoc/apiref/bqoqnz0.html


Alternatively, you can call the python libraries using http://www.mathworks.com/help/techdoc/ref/f16-35614.html#f16-37149.


HTH,

Thomas
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to