Reputation: 75
I have seen this question floating around without any definite answers such as here. I have .mat
data converted from a different data structure and am trying to load it in python using scipy.io.loadmat
. For some files, this approach works perfectly fine, but for others I get this error:
mat = sio.loadmat(i, verify_compressed_data_integrity=False)
File "/Users/aeglick/opt/anaconda3/lib/python3.8/site-packages/scipy-1.7.1-py3.8-macosx-10.9-x86_64.egg/scipy/io/matlab/mio.py", line 226, in loadmat
matfile_dict = MR.get_variables(variable_names)
File "/Users/aeglick/opt/anaconda3/lib/python3.8/site-packages/scipy-1.7.1-py3.8-macosx-10.9-x86_64.egg/scipy/io/matlab/mio4.py", line 390, in get_variables
hdr, next_position = self.read_var_header()
File "/Users/aeglick/opt/anaconda3/lib/python3.8/site-packages/scipy-1.7.1-py3.8-macosx-10.9-x86_64.egg/scipy/io/matlab/mio4.py", line 346, in read_var_header
hdr = self._matrix_reader.read_header()
File "/Users/aeglick/opt/anaconda3/lib/python3.8/site-packages/scipy-1.7.1-py3.8-macosx-10.9-x86_64.egg/scipy/io/matlab/mio4.py", line 108, in read_header
raise ValueError('Mat 4 mopt wrong format, byteswapping problem?')
ValueError: Mat 4 mopt wrong format, byteswapping problem?
I'm not sure what is causing this issue. I save the .mat
files the same way every time so they should all be readable. I also tried h5py
and get a similar error. Are there any suggestions on how I can read my data files?
Upvotes: 0
Views: 1103
Reputation: 23898
You might be saving the .mat files in different "version" formats without realizing it. If your code calls save(...)
without explicitly specifying a format, it uses the default version for your Matlab session, which is a persisted per-user preference that you can set inside the Matlab GUI. And if you don't set a default format in Preferences, the default version that save(...)
uses varies with the version of Matlab.
The differences between MAT-file versions are significant. In particular, v7.3 completely changed the format to an HDF5-based one (which I don't think scipy.io.loadmat
supports). See https://www.mathworks.com/help/matlab/import_export/mat-file-versions.html.
Check your actual .mat
file versions. And if you want your code to be really portable, change the save(...)
calls in your code to explicitly specify the MAT-file version using a '-v<whatever>'
argument.
Upvotes: 1