2 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
好的,我添加了第二個(gè)答案,它有一個(gè)通用的小功能,可以提取內(nèi)部包含任何類型字段的數(shù)據(jù)墊文件,供將來可能需要它的人使用(甚至對(duì)我自己來說,我經(jīng)常處理這個(gè)問題,每次是時(shí)候構(gòu)建一個(gè)臨時(shí)解決方案了……)。
此函數(shù)應(yīng)該能夠接收任何包含結(jié)構(gòu)和嵌套結(jié)構(gòu)的 mat 文件,并返回一個(gè)字典:
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.

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
將 matlab 結(jié)構(gòu)加載到 python 中有點(diǎn)混亂,但 scipy.io.loadmat 方法可以通過一些探索來完成。
請(qǐng)參閱 scipy.io.loadmat 的幫助。他們解釋了它是如何工作的。
從加載文件開始:
import scipy.io as sio my_struct = sio.loadmat(file_name)
然后,從字典中獲取數(shù)據(jù):
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"]
現(xiàn)在,對(duì)于嵌套結(jié)構(gòu)的每個(gè)級(jí)別,您可以使用 .dtype 查看內(nèi)部的下一個(gè)結(jié)構(gòu),然后提取數(shù)據(jù):
data.dtype data[0,0]["daten"]
進(jìn)而:
data[0,0,]["daten"][0,0]["Spannung"]
** 我可能漏掉了你結(jié)構(gòu)中的一個(gè)關(guān)卡,試一試看看它是否不完全適合你的結(jié)構(gòu)。
添加回答
舉報(bào)