MATLAB 中的数据传递

本文详细介绍了Python与MATLAB之间的数据类型映射规则,包括基本数据类型、序列类型和映射类型的转换方法,以及如何处理从Python返回的数据。

List、Tuple 和 Dict 类型

下表表示创建 list、tuple 和 dict 数据类型的命令。左侧的命令从 Python 解释器运行。右侧的命令是 MATLAB 命令。

Python list — [ ]MATLAB py.list
['Robert', 'Mary', 'Joseph']py.list({'Robert','Mary','Joseph'})
[[1,2],[3,4]]py.list({py.list([1,2]),py.list([3,4])})

关于函数 pyargs 的用法,可以看这篇文章。

传递数据到 Python

MATLAB 类型到 Python 类型的映射

当传递 MATLAB 数据作为 Python 的参数时,MATLAB 转换为最能代表 Python 语言的数据类型。

MATLAB Input Argument Type — Scalar Values OnlyResultingPython py. Type

double

single

float
Complex single
Complex double
Complex
int8
uint8
int16
uint16
int32
int
uint32
int64
uint32
int
long (version 2.7 only)
NaNfloat(nan)
Inffloat(inf)

string scalar

char vector

str
<missing> value in stringNone
logicalbool
Structuredict
Python object — py.typetype
function handle @py.module.function, to Python functions onlymodule.function

MATLAB 向量到 Python 的映射

MATLAB Input Argument Type — 1-by-N Vector

 
Resulting Python Type

double

array.array('d')
singlearray.array('f')
int8array.array('b')
uint8array.array('B')
int16array.array('h')
uint16array.array('H')
int32array.array('i')
uint32array.array('I')
int64 (Not supported for Python 2.7 on Windows®)array.array('q')
uint64 (Not supported for Python 2.7 on Windows)array.array('Q')
char array containing values greater than 127 (version 2.7 only)unicode
char vector
string scalar
str
cell vectortuple

处理从 Python 返回的数据

Python 类型到 MATLAB 类型的自动映射

下表展示 MATLAB 如何将 Python 的返回数据转换为 MATLAB 类型

Python Return Type, as Displayed in PythonResulting MATLAB Type — Scalar
boollogical
int (version 2.7 only). For Python versions 3.x int, you must convert explicitly.int64
floatdouble
complexComplex double
All other Python types — typePython object — py.type

显式类型转换

MATLAB 提供下列函数进行显式类型转换

Python Return Type or Protocol, as Displayed in MATLABMATLAB Conversion Function
py.str (version 2.7)

string

char

uint8

py.str (version 3.x)string
char
py.unicodestring
char
Object with __str__ methodchar
py.bytesuint8
py.intdouble
or
int64
py.longdouble
or
int64
py.array.array(可以将任何格式的 py.array.array 转换为 MATLAB 类型)
 

numeric

double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64

Sequence protocol; for example, py.list and py.tuplecell
Mapping protocol; for example, py.dictstruct

访问 Python 容器类型的元素

Python 容器通常是序列类型(list 或 tuple)或映射类型(dict)。 在 Python 中,使用方括号 [] 或 operator.getitem 函数访问容器中的元素。 标量 strings 参数可用于索引容器。

 

Sequence Types

Python 序列类型类似于 MATLAB 元胞数组。使用平滑括号()索引获取子序列。

li = py.list({1,2,3,4});
res = li(2:3)

结果:

res = 

  Python list with no properties.

    [2.0, 3.0]

使用大括号{}获取元素的内容。

res = li{1}

 结果:

res =

     1

Mapping Types

对于映射类型,使用带 key 参数的大括号

patient = py.dict(pyargs('name','John Doe','billing',127));
patient{"billing"}

结果:

ans =

   127

Size and Dimensions

MALTAB 显示系统信息

p = py.sys.path;
class(p)

结果:

ans =

py.list

p 的索引

p(1)
p{1}

结果:

ans = 

  Python list with no properties.

    ['c:\\work']


ans = 

  Python str with no properties.

    c:\work

检查维度:

len = length(p)
sz = size(p)
len =

    11


sz =

     1    11

Array Support

MATLAB 转换序列类型为 1-by-N 列表

Use Zero-Based Indexing for Python Functions

Python 的索引是从零开始的,当调用 Python 函数,如 py.sys.path,该 Python 容器的第一个元素的索引值 x 为 int32(0)。最后一个索引值为 int32(py.len(x)-1)。

参考资料

1.MATLAB 官方文档:https://ww2.mathworks.cn/help/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值