Pol Torrent
Pol Torrent

Reputation: 45

"Can only read from Matlab level 5 MAT-files"

I'm trying to load a file from Matlab into my python using mat4py. I had the following error

ParseError: Unexpected field name length: 43 

when using

import h5py
from numpy import ndarray
data_py = loadmat("data.mat")

I followed the advise here

So,

explore_v73_matfile(file_name="data.mat", fptr=None, path=r"C:/Users/...mypath/data_folder")
data_100E = loadmat("data.mat")

However, now I have a new error:

ParseError: Can only read from Matlab level 5 MAT-files

Unfortunately, I'm not allowed to share data.mat. Does anyone have a clue how to solve this with the little info I can provide?

Upvotes: 2

Views: 2090

Answers (1)

obchardon
obchardon

Reputation: 10790

The MAT-File Format documentation (PDF) indicate that the last binary structure of a MAT-File is the "Level 5 MAT-files".

This document describes the internal format of MATLAB Level 4 and Level 5 MAT-files. Level 4 MATfiles are compatible with versions of MATLAB up to Version 4. Level 5 MAT-files are compatible with MATLAB Versions 5 and up.

But the loadmat.py function indicate in the comment of the line 425-429 (as of march 2021):

# Check mat file format is version 5
# For 5 format we need to read an integer in the header.
# Bytes 124 through 128 contain a version integer and an
# endian test string

And loadmat.py output an error if your MAT-Files is not compatible.

 if maj_val != 1:
        raise ParseError('Can only read from Matlab level 5 MAT-files')

So probably that your MAT-Files has been created a long time ago, or with an old matlab version and cannot be read anymore with mat4py.

You should try to read your MAT-File with matlab and save them again.

Upvotes: 1

Related Questions