Reputation: 1
I have a .mat file with many arrays and try to convert them to numpy arrays but was not successful so far.
from scipy.io import loadmat
import numpy as np
import pandas as pd
mat = loadmat("FanMap_data.mat")
print(mat.keys())
dict_keys(['__header__', '__version__', '__globals__', 'fanmap', 'fanmap1D'])
The file contains two sets of arrays 'fanmap' and 'fanmap1D' of float64 data.
fanmap
consists of 1 array with shape(1,15), 2 arrays with shape (1943,1) and 26 arrays with shape(1943,15)
fanmap1D
consists of 22 arrays of shape(1,15).
I tried to use pd.DataFrame
, but I do not know how to read so many arrays.
I expect to read all arrays and convert them to numpy arrays.
Upvotes: 0
Views: 68
Reputation: 1
mat = loadmat("FanMap_data.mat")
fanmap1D_data = mat['fanmap1D']
fanmap1D_type = fanmap1D_data.dtype
print(fanmap1D_type) # to identify the available keys
fanmap1D_ndata = {n: fanmap1D_data[n][0,0] for n in fanmap1D_type.names}
fanmap1D_Npc = fanmap1D_ndata['Npc']
fanmap1D_Npc is a numpy array. All other arrays can be read in a similar way.
Upvotes: 0
Reputation: 639
You can iterate through the keys of the mat
dictionary and convert each array to a NumPy array. One way to do is as below:
from scipy.io import loadmat
import numpy as np
mat = loadmat("FanMap_data.mat")
fanmap_arrays = {key: np.array(mat['fanmap'][0, 0][key]) for key in mat['fanmap'].dtype.names}
fanmap1D_arrays = {key: np.array(mat['fanmap1D'][0, 0][key]) for key in mat['fanmap1D'].dtype.names}
print("Arrays in 'fanmap':")
for key, value in fanmap_arrays.items():
print(key, value.shape)
print("\nArrays in 'fanmap1D':")
for key, value in fanmap1D_arrays.items():
print(key, value.shape)
Upvotes: 0