1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > matlab调用python函数未定义函数类 从MATLAB调用Python函数

matlab调用python函数未定义函数类 从MATLAB调用Python函数

时间:2018-09-24 20:19:10

相关推荐

matlab调用python函数未定义函数类 从MATLAB调用Python函数

I need to call a Python function from MATLAB. how can I do this?

解决方案

I had a similar requirement on my system and this was my solution:

In MATLAB there is a function called perl.m, which allows you to call perl scripts from MATLAB. Depending on which version you are using it will be located somewhere like

C:\Program Files\MATLAB\Ra\toolbox\matlab\general\perl.m

Create a copy called python.m, a quick search and replace of perl with python, double check the command path it sets up to point to your installation of python. You should now be able to run python scripts from MATLAB.

Example

A simple squared function in python saved as "sqd.py", naturally if I was doing this properly I'd have a few checks in testing input arguments, valid numbers etc.

import sys

def squared(x):

y = x * x

return y

if __name__ == '__main__':

x = float(sys.argv[1])

sys.stdout.write(str(squared(x)))

Then in MATLAB

>> r=python('sqd.py','3.5')

r =

12.25

>> r=python('sqd.py','5')

r =

25.0

>>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。