跟随这个问题,它提出了如何使用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中那样访问它们?
x = scipy.io.loadmat('bla.mat',struct_as_record=True)
会发生什么?请参见 http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html - unutbu