Introduction
A .mat file holds variables saved from
MATLAB, MathWorks’ numerical
computing environment, with its save command: matrices, strings, cell
arrays and structures, each under its variable name. load brings them back
into the workspace. Other tools, such as SciPy and GNU Octave, read and write them
too.
Three layouts are in use, chosen with an option to save: version 4, a
simple format from early MATLAB; version 5 (also written as versions 6 and 7, which add
Unicode text and compression); and version 7.3, which is an
HDF5 file and is needed for variables of 2 GB or
more.
Structure
A version 5 file starts with a 128-byte header: 116 bytes of text
beginning MATLAB 5.0 MAT-file, 8 bytes of subsystem data, the version
number 0x0100 and the two characters IM, which read as
MI when the file was written in the other byte order. Then come data
elements, each an 8-byte tag giving its type and size, then the data. A variable is a
matrix element holding its flags, dimensions, name and values. From version 7 each
variable is usually wrapped in a compressed element, deflated with zlib.
A version 7.3 file is HDF5, with a 512-byte user block in front that
carries the same kind of text header, beginning MATLAB 7.3 MAT-file.
Each variable is an HDF5 dataset or group.
A version 4 file has no file header: it is a run of matrices, each with a 20-byte header giving its type, rows, columns, whether it has an imaginary part, and the length of its name.
The header and data elements page gives every data type, array class and subelement.
Not described
The reference follows MathWorks' MAT-file format document (R2019b).
Known, not yet covered
- Reference: the MCOS object metadata and the
#subsystem#data of version 7.3 files, which hold MATLAB class instances. The v7.3 section gives the rest of the HDF5 layout from the matio library source; matio's MCOS code (mat.c,mat73.c) has the remaining details and was not read. - Viewer: not re-audited in this pass.
Unknown
- Reference: the layout of VAX and Cray numbers (Level 4
M= 2, 3, 4): the MathWorks document names them without a layout and the open-source library matio does not read them. (The Level 4 sparse layout is on the data elements page, from matio.) - Reference: the layout of function handles,
opaqueobjects and other newer MATLAB classes written asmxOPAQUEin version 5 files: not in MathWorks' document.
Links
Software
- MATLAB – MathWorks’ numerical computing environment, which saves and loads MAT-files (Windows, macOS, Linux)
- GNU Octave – free MATLAB-compatible environment that reads and writes MAT-files (Windows, macOS, Linux)
Developers
- scipy.io.loadmat – with
savemat, reads and writes versions 4 and 5 (Python) - h5py – reads version 7.3 files, which are HDF5 (Python)
- matio – reads and writes MAT-files (C)
Reference
- MAT-File Format – MathWorks’ description of versions 4 and 5