Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7211 > unrolled thread

Call python function from Matlab

Started by"nazmul.islam@gmail.com" <nazmul.islam@gmail.com>
First post2011-06-07 22:12 -0700
Last post2011-06-08 09:29 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Call python function from Matlab "nazmul.islam@gmail.com" <nazmul.islam@gmail.com> - 2011-06-07 22:12 -0700
    Re: Call python function from Matlab Adam Przybyla <adam@rybnik.pl> - 2011-06-08 07:19 +0000
      Re: Call python function from Matlab Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-06-08 09:33 +0200
    Re: Call python function from Matlab Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-06-08 09:29 +0200

#7211 — Call python function from Matlab

From"nazmul.islam@gmail.com" <nazmul.islam@gmail.com>
Date2011-06-07 22:12 -0700
SubjectCall python function from Matlab
Message-ID<659556b7-bd7d-415e-919e-e5e8260b6f70@j28g2000vbp.googlegroups.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

[toc] | [next] | [standalone]


#7217

FromAdam Przybyla <adam@rybnik.pl>
Date2011-06-08 07:19 +0000
Message-ID<isn7qt$dgu$1@polsl.pl>
In reply to#7211
nazmul.islam@gmail.com <nazmul.islam@gmail.com> wrote:
> 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.
	... try this: http://pypi.python.org/pypi/pymatlab
Regards
								Adam Przybyla

[toc] | [prev] | [next] | [standalone]


#7219

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-06-08 09:33 +0200
Message-ID<isn8ka$ik0$1@r03.glglgl.eu>
In reply to#7217
Am 08.06.2011 09:19 schrieb Adam Przybyla:
> nazmul.islam@gmail.com<nazmul.islam@gmail.com>  wrote:
>> 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.
> 	... try this: http://pypi.python.org/pypi/pymatlab

Thank you for the link, looks interesting for me.

But AFAICT, the OP wants the other direction.


Thomas

[toc] | [prev] | [next] | [standalone]


#7218

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2011-06-08 09:29 +0200
Message-ID<isn8dp$i5t$1@r03.glglgl.eu>
In reply to#7211
Am 08.06.2011 07:12 schrieb nazmul.islam@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web