Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The generated mat file cannot be loaded by MATLAB #1078

Open
tao-jin opened this issue Jun 18, 2024 · 2 comments
Open

The generated mat file cannot be loaded by MATLAB #1078

tao-jin opened this issue Jun 18, 2024 · 2 comments

Comments

@tao-jin
Copy link

tao-jin commented Jun 18, 2024

#region
//MathNet.Numerics, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null
//MathNet.Numerics.Data.Matlab, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null
#endregion
I use the following code to generate a mat file

   public static string WriteMatFile(double[,] data, string matFile)
    {
        Matrix<double> m = DenseMatrix.OfArray(data);
        using (FileStream fs = new FileStream(matFile, FileMode.Create))
        {
            MatlabWriter.Write(fs, m,Path.GetFileNameWithoutExtension(matFile));
        };
        return matFile;
    }

I encountered an error reading the file using a Python script

import scipy.io as scio 
data = scio.loadmat("test.mat")
print('data:\n',data)     		    
print('datatype:\n',type(data)) 	
print('keys:\n',data.keys)  		
print('keys:\n',data.keys())		
print(data['EKG'])      		   
print('target shape\n',data['EKG'].shape)

F:\home> & C:/Users/jt/AppData/Local/Programs/Python/Python312/python.exe f:/home/test.py
Traceback (most recent call last):
File "f:\home\test.py", line 2, in
data = scio.loadmat("test.mat")
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jt\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\io\matlab_mio.py", line 227, in loadmat
matfile_dict = MR.get_variables(variable_names)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jt\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\io\matlab_mio5.py", line 330, in get_variables
res = self.read_var_array(hdr, process)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jt\AppData\Local\Programs\Python\Python312\Lib\site-packages\scipy\io\matlab_mio5.py", line 290, in read_var_array
return self._matrix_reader.array_from_header(header, process)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "_mio5_utils.pyx", line 665, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
File "_mio5_utils.pyx", line 694, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
File "_mio5_utils.pyx", line 768, in scipy.io.matlab._mio5_utils.VarReader5.read_real_complex
File "_mio5_utils.pyx", line 445, in scipy.io.matlab._mio5_utils.VarReader5.read_numeric
File "_mio5_utils.pyx", line 350, in scipy.io.matlab._mio5_utils.VarReader5.read_element
File "_streams.pyx", line 171, in scipy.io.matlab._streams.ZlibInputStream.read_string
File "_streams.pyx", line 147, in scipy.io.matlab._streams.ZlibInputStream.read_into
File "_streams.pyx", line 134, in scipy.io.matlab._streams.ZlibInputStream._fill_buffer
zlib.error: Error -3 while decompressing data: incorrect data check

@tao-jin
Copy link
Author

tao-jin commented Jun 18, 2024

MATLAB load tips
load("/root/home/test.mat")
error: load: error uncompressing data element (data error from zlib)

@1260963901
Copy link

I met the same question when I use scipy.io.loadmat to load it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants