我有很多.mat
个文件需要带入python并使用。我找到了一种方法来轻松处理.mat
个文件here。
这非常有效但现在我想迭代所有的值。我该怎么做呢?
wObs = 'path/to/matfile.mat'
mat = loadmat(wObs)
mat = mat['wObjs']
#function I wrote to get the field names of the .mat file. Returns a list of field names.
fields = mat_feildnames(wObs, 'wObjs')
#now I want to itterate over the data and do things...
for row in range(len(mat)):
for field in fields:
#field is string value.. how do I make this work?
#this format if field is a valid field name will return what I want.
print(mat[row].field)
>>mat['wObjs']
Returns:
array([<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe5e80>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe5eb8>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7080>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7048>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe70f0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe71d0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe72b0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7518>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe75c0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7668>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe76a0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7748>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe77f0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe78d0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7940>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe79b0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7a20>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7a90>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7b00>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7b70>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7be0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7c50>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7cc0>,
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f0de3fe7d30>], dtype=object)
答案 0 :(得分:0)
我重新阅读了链接到帖子,并且发现我会尝试其中一个较小的响应,看看它做了什么..它有效..但是!以下是我上述问题的答案。
import scipy.io as spio
vig=spio.loadmat(wObs,squeeze_me=True)
for field in fields:
print(vig['wObjs'][field])