2 回答

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

TA貢獻1841條經驗 獲得超3個贊
將 matlab 結構加載到 python 中有點混亂,但 scipy.io.loadmat 方法可以通過一些探索來完成。
請參閱 scipy.io.loadmat 的幫助。他們解釋了它是如何工作的。
從加載文件開始:
import scipy.io as sio my_struct = sio.loadmat(file_name)
然后,從字典中獲取數據:
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"]
** 我可能漏掉了你結構中的一個關卡,試一試看看它是否不完全適合你的結構。
添加回答
舉報