Matlab调用.py文件

下面的例子阐明了如何通过使用Python模块来调用python的方法,其中模块中包含着下面的例子中所使用的函数。
这个例子解释了如何在MATLAB中创建模块。如果你使用Python的文本编辑器来编写mymod.py,首先要确保这个模块处理python搜索路径中。

首先在MATLAB的Editor中创建一个文件,复制如下的命令并把这个文件保存为mymod.py。

# mymod.py
"""Python module demonstrates passing MATLAB types to Python functions"""
def search(words):
    """Return list of words containing 'son'"""
    newlist = [w for w in words if 'son' in w]
    return newlist

def theend(words):
    """Append 'The End' to list of words"""
    words.append('The End')
    return words

在MATLAB的命令提示行中,输入以下命令,来添加当前目录到Python搜索路径中。

if count(py.sys.path,'') == 0
    insert(py.sys.path,int32(0),'');
end


Create an input argument, a list of names, in MATLAB.
N = py.list({'Jones','Johnson','James'});

在调用search函数时,在模块名字和函数名字前面键入“py.”。

names = py.mymod.search(N);

其中,names中保存有py.list类型的值。原先的输入N是没有变化的。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值