python调用matlab文件_python 执行matlab文件

环境:matlab2014b,mac os,python 2.7

1. windows用户可以用win32com,COM只适用于WINDOWS系统,这里没有尝试。

2. 如果是执行简单的命令,可以用matlab2014b提供的引擎,我的在/Applications/MATLAB_R2014b.app/extern/engines/python。

###Matlab Engine for Python

#Call Matlab Function from Python

------------------------------

##Step 1: Installation

#Install with Administrator Privileges

cd "matlabroot\extern\engines\python"

python setup.py install

#Install without Administrator Privileges

cd "matlabroot\extern\engines\python"

python setup.py build --build-base builddir install --install-base installdir

Include 'installdir' in the search path for Python packages

Add 'installdir' to the PYTHONPATH environment variavle

------------------------------

##Step 2: Using Matlab Engine

#Start and quit

import matlab.engine

eng = matlab.engine.start_matlab()

eng.quit()

#Call Matlab Functions:

#Just call with form eng.xxx()

#the function xxx should in the namespace of matlab.

#Asynchronously Call

import matlab.engine

eng = matlab.engine.start_matlab()

future = eng.sqrt(4.0,async=True)

ret = future.result()

print(ret)

#WorkSpace Usage:

import matlab.engine

eng = matlab.engine.start_matlab()

eng.workspace['y'] = x

a = eng.eval('sqrt(y)')

print(a)

#Skills for unsupported features in python

#eng.eval()

import matlab.engine

eng = matlab.engine.start_matlab()

eng.eval("T = readtable('patients.dat');",nargout=0)

#Plot With Matlab:

import matlab.engine

eng = matlab.engine.start_matlab()

data = eng.peaks(100)

eng.mesh(data)

------------------------------

3. python直接调用执行matlab,现有很多工具:

3.1. pymat

没有python2.7的支持

3.2. pymat2

在pymat基础上改良过的。

3.3. mlabwrap

http://mlabwrap.sourceforge.net/

需要用root权限。最初安装不成功,报OSError: [Errno 2] No such file or directory,应该是找不到MATLAB2014b的路径,更改PATH加上MATLAB的安装目录后成功(在setup.py中更改安装参数应该也可以,没有尝试)。export PATH=/Applications/MATLAB_R2014b.app/bin:$PATH

from mlabwrap import mlab的时候又报import mlabraw引入不成功,google之后发现export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2014b.app/bin/maci64:$DYLD_LIBRARY_PATH即可,

这里runmodel.m文件内容是Function result = runmodel() ..... END

调用方式为mlab.runmodel()

解决方案参考了http://stackoverflow.com/questions/13311415/run-a-matlab-script-from-python-pass-args/13316939#13316939

http://sourceforge.net/p/mlabwrap/mailman/message/26145026/

传入参数:

mlab.runmodel('[1,2,3,4,5]',...)注意参数必须为字符串,python会将其转换为各种形式。

3.4. mlab

貌似在mac上不稳定,执行不了后就放弃了。

3.5. python-matlab-bridge

only work on unix, and is based on TCP transmission while messages are decoded in JSON format。

3.6. Nipype

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值