【发布时间】:2010-12-31 09:07:29
【问题描述】:
在this question 询问(并回答)如何读取使用 Scipy 在 Matlab 中创建的 .mat 文件之后,我想知道如何访问导入结构中的字段。
我在 Matlab 中有一个文件,可以从中导入结构:
>> load bla % imports a struct called G
>> G
G =
Inp: [40x40x2016 uint8]
Tgt: [8x2016 double]
Ltr: [1x2016 double]
Relevant: [1 2 3 4 5 6 7 8]
现在我想在 Python 中做同样的事情:
x = scipy.io.loadmat('bla.mat')
>>> x
{'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Jun 07 21:17:24 2006', 'G': array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object), '__globals__': []}
>>> x['G']
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
>>> G = x['G']
>>> G
array([[<scipy.io.matlab.mio5.mat_struct object at 0x0191F230>]], dtype=object)
问题是,如何访问结构 G 的成员:Inp、Tgt、Ltr 和 Relevant,就像在 Matlab 中那样?
【问题讨论】:
-
help(G) 和 dir(G) 说什么(G[0] 也一样)
-
如果你使用
x = scipy.io.loadmat('bla.mat',struct_as_record=True)会发生什么?见docs.scipy.org/doc/scipy/reference/generated/…
标签: python matlab file-io scipy mat-file