我有一个由matlab创建的struct数组,并存储在v7.3格式的mat文件中:
struArray = struct('name', {'one', 'two', 'three'},
'id', {1,2,3},
'data', {[1:10], [3:9], [0]})
save('test.mat', 'struArray', '-v7.3')
Run Code Online (Sandbox Code Playgroud)
现在我想通过python使用h5py读取这个文件:
data = h5py.File('test.mat')
struArray = data['/struArray']
Run Code Online (Sandbox Code Playgroud)
我不知道如何逐个获取结构数据struArray:
for index in range(<the size of struArray>):
elem = <the index th struct in struArray>
name = <the name of elem>
id = <the id of elem>
data = <the data of elem>
Run Code Online (Sandbox Code Playgroud) 我试图从'.mat'v7.3文件中提取所有变量,并将它们转换为NumPy数组.有没有办法一般地执行此操作,最好不需要指定变量名称?如何从h5py.File获取所有当前变量名称,然后检查它们的尺寸?
防爆.
import numpy as np, h5py
file = h5py.File('data.mat','r')
for "all variables in mat file"
...fill numpy array
end
Run Code Online (Sandbox Code Playgroud)