zoukankan      html  css  js  c++  java
  • Windows中使用 Python 调用 Matlab 程序

    https://ww2.mathworks.cn/help/matlab/matlab_external/system-and-configuration-requirements.html

    https://ww2.mathworks.cn/help/matlab/matlab_external/start-the-matlab-engine-for-python.html

    环境变量:

     

    1 Python-Matlab引擎 / Pyhton-Matlab Engine

    首先,需要确保Matlab及Python的配置和安装,利用Matlab提供的setup.py文件安装Python的引擎包,安装步骤及过程如下,

    1. 确保安装可用的Python和Matlab,且两者版本对应,如32位的Matlab需对应32位的Python,同时还需查看Matlab支持的Python版本(目前2016a版支持的Python版本为2.7/…/3.5);

    2. 添加Python目录到环境变量(如果未添加);

    3. 获取Matlab文件夹目录,可通过Matlab命令行窗口输入matlabroot命令返回;

    4. 安装引擎,Windows利用下面的命令(此处路径可能需要修改)进行安装,此处可能需要管理员权限运行。

    1 cd S:Program FilesMATLABR2016bexternenginespython

    2 python setup.py install 

    test.py

    import matlab.engine

    # Basic usage

    int_8 = matlab.int8([1, 2, 3, 4, 5, 6])

    print(int_8)    # [[1, 2, 3, 4, 5, 6]]

    print(int_8.size)   # (1, 6)

    int_8.reshape((2, 3))   # reshape function is different from numpy

    print(int_8)    # [[1, 3, 5], [2, 4, 6]]

    double = matlab.double([[1, 2, 3], [4, 5, 6]])

    print(double)   # [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]

    print(double[0])    # [1.0, 2.0, 3.0]

    print(double[1][2]) # 6.0

    Python-Matlab调用m文件

    如何使用Python调用m来进行计算并获得返回结果,首先定义以下的m文件,在被调用的m文件中再调用下一个m文件,使用的m文件如下:

    定义入口函数callentry,接收两个参数,随后对两个参数分别在内部进行加和乘操作,再调用外部另一个m文件的callsub函数进行相减操作,将返回的结果保存在数组r中返回。

    callentry.m 代码

    function [x, y, z] = callentry(a, b);

    x = add(a, b)

    y = mul(a, b)

    z = callsub(a, b)

    end

    function l = mul(m, n);

    l=m*n;

    end

    function l = add(m, n);

    l=m+n;

    end

    callsub.m 代码

    function r = callsub(a, b);

    r = a-b;

    end

    在Python中,运行如下代码

    import matlab.engine

    eng = matlab.engine.start_matlab()

    print(eng.callentry(7.7, 2.1, nargout=3))

    eng.quit()

    输出:

    x =

        9.8000

    y =

       16.1700

    z =

        5.6000

    (9.8, 16.17, 5.6)

    Note: 值得注意的是,此处需要设置nargout参数,当未设置时默认为1,即默认只返回1个参数,当知道Matlab返回参数的数量时,通过nargout进行设置来获取所有需要的参数。无参数返回时请设为0。

    在第一次运行生成实例时会较慢,因为需要启动Matlab引擎,最终得到输出如下,可以看到,Matlab的console界面显示的结果在Python中也会输出,最后得到的结果是列表形式的Python数据。

    import matlab.engine
    
    # Basic usage
    int_8 = matlab.int8([1, 2, 3, 4, 5, 6])
    print(int_8)    # [[1, 2, 3, 4, 5, 6]]
    print(int_8.size)   # (1, 6)
    int_8.reshape((2, 3))   # reshape function is different from numpy
    print(int_8)    # [[1, 3, 5], [2, 4, 6]]
    
    double = matlab.double([[1, 2, 3], [4, 5, 6]])
    print(double)   # [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
    print(double[0])    # [1.0, 2.0, 3.0]
    print(double[1][2]) # 6.0
    
    # 对于数组的切片,Matlab的array与Python的list也有所不同,
    # 官网给出的解释在于,Matlab数组切片返回的是一个视图,
    # 而不是像Python中返回一个浅拷贝。
    # Slice array
    py = [[1, 2, 3], [4, 5, 6]]
    mt = matlab.int32([[1, 2, 3], [4, 5, 6]])
    py[0] = py[0][::-1]
    mt[0] = mt[0][::-1]
    # Slicing a Matlab array returns a view instead of a shallow copy
    print(py)   # [[3, 2, 1], [4, 5, 6]]
    print(mt)   # [[3, 2, 3], [4, 5, 6]]
    
    
    # Python-Matlab基本操作
    # import matlab.engine
    eng = matlab.engine.start_matlab()
    # 调用sqrt()函数
    print(eng.sqrt(4.))     # 2.0
    # 利用引擎实例调用plot函数进行画图,传入的参数必须是Matlab类型参数
    eng.plot(matlab.int32([1, 2, 3, 4]), matlab.int32([1, 2, 3, 4]))
    
    # 当需要执行某些Matlab命令时,可以利用eval函数对其进行输入,
    # 下面的方法画另外一条直线,其中nargout参数为设置输出返回参数的数量,默认为1。
    # 无参数返回时需要设置为0。
    eng.eval("hold on", nargout=0)
    eng.eval("plot([4, 3, 2, 1], [1, 2, 3, 4])", nargout=0)
    
    eng.eval("x = 3", nargout=0)
    eng.eval("y = 41", nargout=0)
    eng.eval("z = [213, 123]", nargout=0)
    print(eng.workspace)
    print(eng.workspace['x'], eng.workspace['z'])
    """
      Name      Size            Bytes  Class     Attributes
    
      x         1x1                 8  double
      y         1x1                 8  double
      z         1x2                16  double
    
    3.0 [[213.0,123.0]]
    """
    input("Press Enter to exit.")
    eng.quit()
    

      输出:

    [[1,2,3,4,5,6]]
    (1, 6)
    [[1,3,5],[2,4,6]]
    [[1.0,2.0,3.0],[4.0,5.0,6.0]]
    [1.0,2.0,3.0]
    6.0
    [[3, 2, 1], [4, 5, 6]]
    [[3,2,3],[4,5,6]]
    2.0

    x =

    3


    y =

    41


    z =

    213 123

    Name Size Bytes Class Attributes

    x 1x1 8 double
    y 1x1 8 double
    z 1x2 16 double


    3.0 [[213.0,123.0]]
    Press Enter to exit.

  • 相关阅读:
    Linq to Json
    CoreData 增删改查
    ios中的coredata的使用
    更安全的HTTPS
    Block编程注意的问题
    iOS 网络编程 TCP/UDP HTTP
    imageNamed 、imageWithContentsOfFile、 initWithContentsFile区别
    调试解决iOS内存泄漏
    iOS中的#import和class区别
    何时使用copy,strong,weak,assign关键字 定义属性
  • 原文地址:https://www.cnblogs.com/jeshy/p/11561093.html
Copyright © 2011-2022 走看看