mathnet / mathnet-numerics

Math.NET Numerics
http://numerics.mathdotnet.com
MIT License
3.51k stars 898 forks source link

The generated mat file cannot be loaded by MATLAB #1078

Open tao-jin opened 5 months ago

tao-jin commented 5 months ago

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 commented 5 months ago

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

1260963901 commented 1 month ago

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