文章目录

  • ★★★★★ 1. 环境
  • ★★★★★ 2. 使用方法
  • 2.1 数据映射问题
  • MATLAB 向Python传递数据
  • MATLAB 类型到 Python 类型的映射
  • MATLAB 向量到 Python 的映射
  • 传递矩阵和多维数组
  • 处理从 Python 返回的数据
  • Python 类型到 MATLAB 类型的自动映射
  • 显式类型转换
  • ★★★★★ 2.2 函数调用
  • ★★★★★ 2.3 类调用
  • ★★★★★ 3. 案例


★★★★★ 1. 环境

环境配置参见MATLAB 与 python 的交互

★★★★★ 2. 使用方法

要调用 Python 方法或函数,请键入 py.,然后键入模块名、函数名和参数,即:

py.代码文件名或模块.函数名或类名(参数)

在大多数情况下,MATLAB 自动将输入参数转换为 Python 类型。

2.1 数据映射问题
MATLAB 向Python传递数据
MATLAB 类型到 Python 类型的映射

将MATLAB数据作为参数传递给 Python时,MATLAB 会将数据转换为最适合在Python 语言中表达该数据的类型。

matlab 调用python matlab调用Python缓存_matlab 调用python

MATLAB 向量到 Python 的映射

matlab 调用python matlab调用Python缓存_matlab_02

传递矩阵和多维数组

将实数数组或逻辑数组传递给 Python 函数时,MATLAB 会自动将数据转换为 Python memoryview 对象。如果 Python 函数的输出实现 Python 缓冲区协议并且是实数或逻辑值,MATLAB 将显示:

  • 实际的 Python 类型
  • 底层数据
  • 对应的 MATLAB 转换函数。使用此函数可将 Python 对象完全转换为 MATLAB 数组

例如,假设您在模块 pyModule 中调用 Python 函数,该函数返回类型为 pyType 的变量并具有以下值:

p = 
  Python pyType:

     8     1     6
     3     5     7
     4     9     2

    Use details function to view the properties of the Python object.

    Use double function to convert to a MATLAB array.

要将 p 转换为 MATLAB 矩阵 P,请键入:P = double(p) 如果需要 p 的 Python 属性的特定信息,请键入:details(p)

参考https://ww2.mathworks.cn/help/matlab/matlab_external/passing-data-to-python

处理从 Python 返回的数据
Python 类型到 MATLAB 类型的自动映射

matlab 调用python matlab调用Python缓存_Python_03

显式类型转换

MATLAB 提供下列函数,用于手动将 Python 数据类型转换为 MATLAB 类型。

matlab 调用python matlab调用Python缓存_matlab_04


参考https://ww2.mathworks.cn/help/matlab/matlab_external/handling-data-returned-from-python.html

★★★★★ 2.2 函数调用

假设已有python代码为:

# File Name:py2mtb_test.py
def test(a):
    t = math.sin(a)
    return t

将matlab工作空间切换至该代码所在目录下,matlab中执行情况如下:

>> py.py2mtb_test.test(pi/4)
ans =
    0.7071
>>

如果传入一个数组,将python代码改为:

# File Name:py2mtb_test.py

def test(a):
    t = [0]*2;
    t[0] = math.sin(a[0])
    t[1] = math.sin(a[1])
    return t

此时,matlab中调用的python程序,依然是修改前的,所以需要重载python,执行如下命令:

>> clear classes
>> obj = py.importlib.import_module('py2mtb_test');
>> py.importlib.reload(obj);

matlab中执行情况如下:

>> clear classes
>> obj = py.importlib.import_module('py2mtb_test');
>> py.importlib.reload(obj);
>> py.py2mtb_test.test([pi/4 pi/3])
ans = 
  Python list (不带属性)。

    [0.7071067811865476, 0.8660254037844386]

发现返回的是Python list类型,查表发现matlab中可将其转为cell

>> cell(ans)
ans =
  1×2 cell 数组
    {[0.7071]}    {[0.8660]}

但是我想要的是在matlab中的double型向量形式,修改python程序如下:

# File Name:py2mtb_test.py

def test(a):
    t = [0]*2;
    t[0] = math.sin(a[0])
    t[1] = math.sin(a[1])
    return np.array(t)

注意,这里需要已经安装np模块,如果没有安装请DOS执行pip install np

matlab执行情况如下:

>> clear classes
>> obj = py.importlib.import_module('py2mtb_test');
>> py.importlib.reload(obj);
>> py.py2mtb_test.test([pi/4 pi/3])
ans = 
  Python ndarray:

    0.7071    0.8660
    使用 details 函数查看 Python 对象的属性。

    使用 double 函数转换为 MATLAB 数组。
>> double(py.py2mtb_test.test([pi/4 pi/3]))
ans =
    0.7071    0.8660
>>

matlab 调用python matlab调用Python缓存_MATLAB_05

★★★★★ 2.3 类调用

python类如下:

# File Name:py2mtb_classtest.py
import math

class testclass():
    def __init__(self):
        self.x_ = 1.5707
    def test(self):
        print('sin(1.5707) = ',math.sin(self.x_))

testc = testclass()
testc.test()

matlab执行情况如下:

>> clear classes
>> obj = py.importlib.import_module('py2mtb_classtest');
>> py.importlib.reload(obj);
sin(1.5707) =  0.9999999953605743
>> test_ = py.py2mtb_classtest.testclass
test_ = 
  Python testclass - 属性:

    x_: 1.5707
    <py2mtb_classtest.testclass object at 0x0000000048140D88>
>> test_.test()
sin(1.5707) =  0.9999999953605743
>>

参考自这儿

★★★★★ 3. 案例

test.py

import numpy as np;
def test_test():
    a=[1,2,3,4,5,6];
    lists=[[]for i in range(3)];
    count_ = 0
    for i in range(3):
        temp=[0]*2
        for j in range(2):
            temp[j] = a[count_];
            count_ = count_ +1;
        print(temp)
        lists[i]=temp;
    return np.array(lists);

matlab_demo.m

clear,clc;


a = 1;
b=[1,2;2,1];
[c,d]=test1(a,b)

double(py.test.test_test()) % 忽略其他,看这里!!!!!

function [c,d] = test1(a,b)
    c=a;
    d=b;
end

matlab 调用python matlab调用Python缓存_matlab_06