I have a struct array created by matlab and stored in v7.3 format mat file:
struArray = struct('name', {'one', 'two', 'three'},
'id', {1,2,3},
'data', {[1:10], [3:9], [0]})
save('test.mat', 'struArray', '-v7.3')
Now I want to read this file via python using h5py:
data = h5py.File('test.mat')
struArray = data['/struArray']
I have no idea how to get the struct data one by one from 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>
Best Answer
Matlab 7.3 file format is not extremely easy to work with h5py. It relies on HDF5 reference, cf. h5py documentation on references.
To read
struArray(i).id
:Notice that Matlab stores a number as an array of size (1, 1), hence the final
[0, 0]
to get the number.To read
struArray(i).data
:To read
struArray(i).name
, it is necessary to convert the array of integers to string: