I have a big matrix in MATLAB, which I need to save it in order to use it in python. However, when I save it, MATLAB can't save it except with -v7.3
but that file version can't be read in Python using the following:
import scipy.io as spio
Data = spio.loadmat('example_file.mat', squeeze_me=True)
A = Data[‘B’]
So it’s required to read that data using h5py
, as below:
import h5py
Data = h5py.File(‘example_file.mat’. ‘r’)
A = Data[‘B’]
In that way of using h5py
, the matrix A
can be read and shown with an extension of H5 and I can’t see the matrix numbers as the file A resulted from the first way (which is using import scipy.io as spio
) .
Is it possible to save a big matrix from MATLAB in similar way to saving a small matrix and then read it in Python using import scipy.io as spio
?
Or the other option, is it possible to read a big matrix saved in MATLAB with –v7.3
in Python and read the matrix with same extension using import scipy.io as spio
EDIT
Here is an example of what happens when saving the matrix in format 7.3
In MATLAB, we can generate the matrix and save it in version -v7.3:
example = randn(16,200) + i*randn(16,200);
save('example.mat', '-v7.3')
I saved it here in v7.3 because if the matrix is very big, I can't save it except in that version !!
Then, to read that file in python, we must use the h5py
as below:
import h5py as h5
data_try = hs.File('example.mat', 'r')
A = np.array(data_try )
First, the matrix A
is not of size of (16,200)
in python!! it's of size (200,16)
Second, when I used for example the matrix A
as an input of neural network, it shows an error of "can't cast from structure to non-structure, except if the structure only has a single field python"
. I think that is because the dtype of matrix A
in python is something like that [('real','<f8'), ('imag','<f8')]
. !!
all that is different when saving the matrix from matlab in version newer than v7.3 and then be able to read it using the import scipy.io as spio
which gives us all simply a matrix of dtype : complex