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.
Links
Software
- MATLAB (Windows, macOS, Linux)
- GNU Octave (Windows, macOS, Linux)
Developers
- scipy.io.loadmat and
savemat, Python, for versions 4 and 5 - h5py, Python, for version 7.3 files
- matio, C
Reference
- MAT-File Format, MathWorks, for versions 4 and 5