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])})
传递数据到 Python
MATLAB 类型到 Python 类型的映射
当传递 MATLAB 数据作为 Python 的参数时,MATLAB 转换为最能代表 Python 语言的数据类型。
MATLAB Input Argument Type — Scalar Values Only ResultingPython py.Typedouble
single
float Complex single
Complex doubleComplex int8
uint8
int16
uint16
int32int uint32
int64
uint32int
long (version 2.7 only)NaN float(nan) Inf float(inf) string scalar
char vector
str <missing> value in string None logical bool Structure dict Python object — py.type type function handle @py.module.function, to Python functions only module.function MATLAB 向量到 Python 的映射
MATLAB Input Argument Type — 1-by-N Vector
Resulting Python Type double
array.array('d') single array.array('f') int8 array.array('b') uint8 array.array('B') int16 array.array('h') uint16 array.array('H') int32 array.array('i') uint32 array.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 scalarstr cell vector tuple
处理从 Python 返回的数据
Python 类型到 MATLAB 类型的自动映射
下表展示 MATLAB 如何将 Python 的返回数据转换为 MATLAB 类型
Python Return Type, as Displayed in Python Resulting MATLAB Type — Scalar bool logical int (version 2.7 only). For Python versions 3.x int, you must convert explicitly. int64 float double complex Complex double All other Python types — type Python object — py.type 显式类型转换
MATLAB 提供下列函数进行显式类型转换
Python Return Type or Protocol, as Displayed in MATLAB MATLAB Conversion Function py.str (version 2.7) string
char
uint8
py.str (version 3.x) string
charpy.unicode string
charObject with __str__ method char py.bytes uint8 py.int double
or
int64py.long double
or
int64py.array.array(可以将任何格式的 py.array.array 转换为 MATLAB 类型)
numeric
double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64Sequence protocol; for example, py.list and py.tuple cell Mapping protocol; for example, py.dict struct
访问 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 = 1Mapping Types
对于映射类型,使用带 key 参数的大括号
patient = py.dict(pyargs('name','John Doe','billing',127)); patient{"billing"}结果:
ans = 127Size and Dimensions
MALTAB 显示系统信息
p = py.sys.path; class(p)结果:
ans = py.listp 的索引
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 11Array 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/
本文详细介绍了Python与MATLAB之间的数据类型映射规则,包括基本数据类型、序列类型和映射类型的转换方法,以及如何处理从Python返回的数据。
647

被折叠的 条评论
为什么被折叠?



