python和matlab混合编程,并解决error “ImportError: No module named matlab.engine”


python和matlab混合编程,并解决error “ImportError: No module named matlab.engine”

  • 1.我的 matlab版本为2016b,python版本为3.5.4 注意:matlab支持的python版本为2.7,3.3, 3.4 ,3.5,目前不支持3.6 查看自己的python版本:python -V

木里

如何切换自己的python版本,请参考一下方案:
https://www.cnblogs.com/beginner-boy/p/7248399.html

  • 2.在pycharm中,添加“matlab”

木里

  • 3.在matlab安装路径中,找到”R2016b\extern\engines\python”,每个人安装路径不一样,找到末尾一致路径即可

木里

  • 4.windows+r打开cmd,依次执行:d: –>cd D:\Program Files\MATLAB\R2016b\extern\engines\python,即切换路径到matlab的python的setup.py下

木里

  • 5.一次执行以下命令:

(1) python setup.py build –build-base=”builddir” install
(2) python setup.py install –prefix=”installdir”
(3) python setup.py build –build-base=”builddir” install –prefix=”installdir”
(4) python setup.py install –user
注意:我的python为3.5.4版本,电脑还安装了3.6.2版本的,
为了区别,在cmd中,3.5.4版本的执行为:python35,执行:
(1) python35 setup.py build –build-base=”builddir” install
(2) python35 setup.py install –prefix=”installdir”
(3) python35 setup.py build –build-base=”builddir” install –prefix=”installdir”
(4) python35 setup.py install –user

恩,好处就是:When you install with –user, you do not need to add your home folder to PYTHONPATH.
参考为:https://cn.mathworks.com/help/matlab/matlab_external/install-matlab-engine-api-for-python-in-nondefault-locations.html

  • 6:在matlab中,查看是否这有四个文件夹

木里

  • 7:执行以下测试代码
#coding=utf-8
import matlab.engine
from numpy import *

if __name__ == '__main__':
    eng = matlab.engine.start_matlab('MATLAB_R2016b')
    A = matlab.double([[1,2],[5,6]])
    print(type(A),A.size,A)
    print(eng.eig(A))
    eng.quit()
    pass
  • 8:执行结果为:

木里

  • 执行成功!

转载自:https://blog.csdn.net/mr_muli/article/details/78823734

You may also like...