将带有structs的.mat文件提取到Python中



我是python的新手,在提取文件数据时遇到了困难。所以我有一个.mat文件,它在Matlab中有以下输出:

d= open('tesla.mat')

d=

带字段的结构:

diga: [1×1 struct]

d.diga.daten

ans=

带字段的结构:

Programmdauer: [1×78 double]
SOCVec: [1×78 double]
Spannung: [1×78 double]
SpannungVec: [1×78 double]
Strom: [1×78 double]
StromVec: [1×78 double]
TemperaturVec: [1×78 double]
ThermischLeistung: [1×78 double]
ThermischLeistungVec: [1×78 double]

d.diga.daten.Spannung

ans=

第1列至第20列

4.0544    4.2100    4.2133    4.2149    4.2159    4.2168    4.2175    4.2181    4.2186    4.2191    4.0332    4.0265    4.0247    4.0235    4.0227    4.0223    4.0219    4.0217    4.0215    4.0213

第21列至第40列

4.0211    4.0210    4.0208    4.0552    4.0559    4.0561    4.0563    4.0564    4.0564    4.0564    4.0564    4.0564    4.0564    4.0564    4.0563    4.0563    4.0563    4.0563    4.0563    4.0563

第41列至第60列

4.0562    4.0562    4.0562    4.0562    4.0562    4.0562    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0561    4.0560    4.0560    4.0560    4.0560    4.0560

第61列至第78列

4.0560    4.0560    4.0560    4.0560    4.0560    4.0560    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559    4.0559

我想在python中解析这些数据,并提取这个特定的变量,例如Spannung数据。python中的输出如下:

{'__header__': b'MATLAB 5.0 MAT-file, Platform: x86_64-pc-linux-gnu, Created by: libmatio v1.5.9 on Mon Jul  6 20:29:51 2020', '__version__': '1.0', '__globals__': [], 'diga': array([[(array([[(array([[0.        , 0.10464944, 0.21018082, 0.31937647, 0.42176473,
0.53570583, 0.6428233 , 0.74994078, 0.85705826, 0.96417574,
1.0681423 , 1.17389392, 1.28139943, 1.40048611, 1.52559419,
1.64743952, 1.76928485, 1.89113018, 2.        , 2.12290829,
2.24581658, 2.36872488, 2.49163317, 2.59454399, 2.69687044,
2.81347033, 2.93308651, 3.05714129, 3.17464839, 3.3055228 ,
3.45267061, 3.58849168, 3.77533445, 3.88176795, 3.998157  ,
4.19674602, 4.34787781, 4.5       , 4.69631901, 4.83781901,
4.99708256, 5.20507697, 5.37073747, 5.5       , 5.6080527 ,
5.78533992, 5.95166902, 6.09878942, 6.28218433, 6.4540618 ,
6.59066557, 6.69357254, 6.8539116 , 6.95566133, 7.08274939,
7.19399435, 7.3374617 , 7.44876399, 7.58496883, 7.70381522,
7.83578215, 7.94780886, 8.08803463, 8.19780798, 8.34935696,
8.45694731, 8.58460976, 8.69666891, 8.83509531, 8.94704739,
9.08658847, 9.20783487, 9.33469661, 9.44355668, 9.59016133,
9.6962619 , 9.85349258, 9.95928693]]), array([[88.3       , 88.30430713, 88.30865056, 88.3131448 , 88.31735886,
88.32204841, 88.32645712, 88.33086583, 88.33527453, 88.33968324,
88.34053444, 88.33956722, 88.33858396, 88.33749478, 88.33635052,
88.3352361 , 88.33412169, 88.33300727, 88.33201153, 88.33088739,
88.32976326, 88.32863912, 88.32751498, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846, 88.32743846, 88.32743846,
88.32743846, 88.32743846, 88.32743846]]), array([[4.05436068, 4.21002405, 4.21327229, 4.21486158, 4.21591229,
4.2168056 , 4.21748722, 4.21807606, 4.21860772, 4.21910362,
4.03324528, 4.0265357 , 4.02467542, 4.02350034, 4.02273807,
4.02225604, 4.02191832, 4.02166347, 4.02147603, 4.02129491,
4.02113115, 4.020979  , 4.02083454, 4.05516985, 4.05587307,
4.05613862, 4.05627884, 4.05635271, 4.05638517, 4.0563976 ,
4.0563957 , 4.05638637, 4.05636811, 4.05635644, 4.05634327,
4.05632056, 4.05630343, 4.05628649, 4.05626516, 4.05625018,
4.05623371, 4.05621284, 4.0561967 , 4.05618439, 4.0561743 ,
4.05615809, 4.05614328, 4.05613048, 4.0561149 , 4.05610068,
4.05608961, 4.05608141, 4.05606887, 4.05606105, 4.05605142,
4.05604313, 4.05603261, 4.05602458, 4.05601491, 4.0560066 ,
4.05599751, 4.05598991, 4.05598053, 4.0559733 , 4.05596346,
4.05595658, 4.05594852, 4.05594153, 4.05593303, 4.05592624,
4.05591789, 4.05591073, 4.05590333, 4.05589706, 4.05588872,
4.05588277, 4.05587405, 4.05586825]]), array([[4.05436068, 4.21002405, 4.21327229, 4.21486158, 4.21591229,
4.2168056 , 4.21748722, 4.21807606, 4.21860772, 4.21910362,
4.03324528, 4.0265357 , 4.02467542, 4.02350034, 4.02273807,
4.02225604, 4.02191832, 4.02166347, 4.02147603, 4.02129491,
4.02113115, 4.020979  , 4.02083454, 4.05516985, 4.05587307,
4.05613862, 4.05627884, 4.05635271, 4.05638517, 4.0563976 ,
4.0563957 , 4.05638637, 4.05636811, 4.05635644, 4.05634327,
4.05632056, 4.05630343, 4.05628649, 4.05626516, 4.05625018,
4.05623371, 4.05621284, 4.0561967 , 4.05618439, 4.0561743 ,
4.05615809, 4.05614328, 4.05613048, 4.0561149 , 4.05610068,
4.05608961, 4.05608141, 4.05606887, 4.05606105, 4.05605142,
4.05604313, 4.05603261, 4.05602458, 4.05601491, 4.0560066 ,
4.05599751, 4.05598991, 4.05598053, 4.0559733 , 4.05596346,
4.05595658, 4.05594852, 4.05594153, 4.05593303, 4.05592624,
4.05591789, 4.05591073, 4.05590333, 4.05589706, 4.05588872,
4.05588277, 4.05587405, 4.05586825]]), array([[ 0. ,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5, -1. ,
-1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. ,
-1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ]]), array([[ 0. ,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5, -1. ,
-1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. ,
-1. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,
0. ]]), array([[25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.,
25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25., 25.]]), array([[0.00000000e+00, 6.85992309e-01, 7.03449743e-01, 7.10867829e-01,
7.15615588e-01, 7.19174707e-01, 7.21389575e-01, 7.22912024e-01,
7.24007563e-01, 7.24847258e-01, 4.45033989e-02, 3.85461248e-02,
3.58360767e-02, 3.53935152e-02, 3.56460808e-02, 3.59131697e-02,
3.60769536e-02, 3.61500744e-02, 3.61654153e-02, 3.61491829e-02,
3.61153062e-02, 3.60739446e-02, 3.60309650e-02, 9.69429070e-04,
5.35032457e-04, 3.58732548e-04, 2.92579042e-04, 2.64021521e-04,
2.49466440e-04, 2.38203495e-04, 2.27951943e-04, 2.19594624e-04,
2.09204995e-04, 2.03745280e-04, 1.98105959e-04, 1.89197221e-04,
1.82955847e-04, 1.77092643e-04, 1.70083570e-04, 1.65383061e-04,
1.60410350e-04, 1.54374091e-04, 1.49900760e-04, 1.46597195e-04,
1.43952210e-04, 1.39825410e-04, 1.36175399e-04, 1.33110819e-04,
1.29488217e-04, 1.26275854e-04, 1.23838901e-04, 1.22066587e-04,
1.19407152e-04, 1.17780340e-04, 1.15810875e-04, 1.14141001e-04,
1.12057839e-04, 1.10493479e-04, 1.08637272e-04, 1.07067274e-04,
1.05375319e-04, 1.03979386e-04, 1.02281688e-04, 1.00989373e-04,
9.92555314e-05, 9.80583899e-05, 9.66725802e-05, 9.54858692e-05,
9.40565860e-05, 9.29290483e-05, 9.15576202e-05, 9.03953350e-05,
8.92071874e-05, 8.82095618e-05, 8.68967397e-05, 8.59677815e-05,
8.46224613e-05, 8.37375088e-05]]), array([[0.00000000e+00, 6.85992309e-01, 7.03449743e-01, 7.10867829e-01,
7.15615588e-01, 7.19174707e-01, 7.21389575e-01, 7.22912024e-01,
7.24007563e-01, 7.24847258e-01, 4.45033989e-02, 3.85461248e-02,
3.58360767e-02, 3.53935152e-02, 3.56460808e-02, 3.59131697e-02,
3.60769536e-02, 3.61500744e-02, 3.61654153e-02, 3.61491829e-02,
3.61153062e-02, 3.60739446e-02, 3.60309650e-02, 9.69429070e-04,
5.35032457e-04, 3.58732548e-04, 2.92579042e-04, 2.64021521e-04,
2.49466440e-04, 2.38203495e-04, 2.27951943e-04, 2.19594624e-04,
2.09204995e-04, 2.03745280e-04, 1.98105959e-04, 1.89197221e-04,
1.82955847e-04, 1.77092643e-04, 1.70083570e-04, 1.65383061e-04,
1.60410350e-04, 1.54374091e-04, 1.49900760e-04, 1.46597195e-04,
1.43952210e-04, 1.39825410e-04, 1.36175399e-04, 1.33110819e-04,
1.29488217e-04, 1.26275854e-04, 1.23838901e-04, 1.22066587e-04,
1.19407152e-04, 1.17780340e-04, 1.15810875e-04, 1.14141001e-04,
1.12057839e-04, 1.10493479e-04, 1.08637272e-04, 1.07067274e-04,
1.05375319e-04, 1.03979386e-04, 1.02281688e-04, 1.00989373e-04,
9.92555314e-05, 9.80583899e-05, 9.66725802e-05, 9.54858692e-05,
9.40565860e-05, 9.29290483e-05, 9.15576202e-05, 9.03953350e-05,
8.92071874e-05, 8.82095618e-05, 8.68967397e-05, 8.59677815e-05,
8.46224613e-05, 8.37375088e-05]]))]],
dtype=[('Programmdauer', 'O'), ('SOCVec', 'O'), ('Spannung', 'O'), ('SpannungVec', 'O'), ('Strom', 'O'), ('StromVec', 'O'), ('TemperaturVec', 'O'), ('ThermischLeistung', 'O'), ('ThermischLeistungVec', 'O')]),)]],
dtype=[('daten', 'O')])}

用python编写哪些代码来提取特定的数组数据。

好的,我添加了第二个答案,它有一个通用的小函数,可以提取里面有任何类型字段的数据表文件,供将来可能需要它的人使用(甚至对我自己来说,我经常处理这个问题,每次我都会构建一个特别的解决方案…(。

这个函数应该能够接收任何包含structs和嵌套structs的mat文件,并返回一个字典:

import scipy.io as sio
def load_from_mat(filename=None, data={}, loaded=None):
if filename:
vrs = sio.whosmat(filename)
name = vrs[0][0]
loaded = sio.loadmat(filename,struct_as_record=True)
loaded = loaded[name]
whats_inside = loaded.dtype.fields
fields = list(whats_inside.keys())
for field in fields:
if len(loaded[0,0][field].dtype) > 0: # it's a struct
data[field] = {}
data[field] = load_from_mat(data=data[field], loaded=loaded[0,0][field])
else: # it's a variable
data[field] = loaded[0,0][field]
return data
# and then just call the function
my_file = r"C:Users.......data.mat"
data = load_from_mat(filename=my_file) # Don't worry about the other input vars (data, loaded), there are used in the recursion.    

将matlab结构加载到python中有点麻烦,但scipy.io.loadmat方法只需进行一些探索即可完成。

请参阅scipy.io.loadmat的帮助。它们解释了它的工作原理。

从加载文件开始:

import scipy.io as sio
my_struct = sio.loadmat(file_name)

然后,从dict:中获取数据

my_struct.keys() # this will show you the var name of your data, in your case diga (if I understand correctly)
data = my_struct["diga"]

现在,对于每一层嵌套结构,您都可以使用.dtype查看内部的下一个结构,然后提取数据:

data.dtype
data[0,0]["daten"]

然后:

data[0,0,]["daten"][0,0]["Spannung"]

**我可能在你的结构中错过了一个级别,玩它看看它是否不完全适合你的结构。

相关内容

  • 没有找到相关文章

最新更新