This page follows MathWorks’ MAT-File Format document (the R2019b PDF, read 2026-11-12 from an archived copy), which describes Level 5 files (MATLAB 5 and later) and the older Level 4 files, and the MAT-file versions page. Numbers are in the byte order of the machine that wrote the file; the header’s endian indicator tells a reader whether to swap.
Header
| Offset | Size | Field |
|---|---|---|
0 | 116 | Descriptive text, such as MATLAB 5.0 MAT-file, Platform: SOL2, Created on: …. Writers must put non-zero bytes in the first four: MATLAB takes a zero there to mean a Level 4 file. |
116 | 8 | Subsystem data offset: zeros or spaces when unused. |
124 | 2 | Version, 0x0100. |
126 | 2 | Endian indicator: the characters MI written as a 16-bit number, so they read IM in a little-endian file. |
Data elements
After the header, the file is a series of data elements, each an 8-byte tag and its
data, padded to a multiple of 8 bytes. The byte count includes that padding for
miMATRIX elements and leaves it out for all other types.
| Form | Tag |
|---|---|
| Normal | Data type (4 bytes), number of bytes (4 bytes), then the data. |
| Small element | For 1 to 4 bytes of data: number of bytes (2) and data type (2) in the first 4 bytes, the data in the next 4. A reader sees it when the upper 16 bits of the first 4-byte word are not 0. |
| Type | Name | Type | Name |
|---|---|---|---|
| 1 | miINT8 | 9 | miDOUBLE |
| 2 | miUINT8 | 10, 11 | Reserved |
| 3 | miINT16 | 12 | miINT64 |
| 4 | miUINT16 | 13 | miUINT64 |
| 5 | miINT32 | 14 | miMATRIX: an array, with subelements |
| 6 | miUINT32 | 15 | miCOMPRESSED: zlib data holding one more element |
| 7 | miSINGLE | 16 | miUTF8 |
| 8 | Reserved | 17, 18 | miUTF16, miUTF32 |
MATLAB compresses each variable separately with zlib: the whole
miMATRIX element goes into a buffer after an miCOMPRESSED
tag whose byte count is the compressed size. The versions page says compression
began with Version 7 files. UTF-16 and UTF-32 data are in the byte order the endian
indicator gives.
miMATRIX subelements
| Subelement | Type | Meaning |
|---|---|---|
| Array flags | miUINT32 × 2 | First word: class in the low byte, flags in the next (08 complex, 04 global, 02 logical). Second word: nzmax for sparse arrays. |
| Dimensions | miINT32 | At least two dimensions. |
| Array name | miINT8 | The variable name. |
| Contents | … | Depends on the class, below. |
| Class | Name | Contents |
|---|---|---|
| 1 | mxCELL_CLASS | One miMATRIX per cell. |
| 2 | mxSTRUCT_CLASS | Field name length (miINT32, always a small element in MATLAB’s files; 32, that is 31 characters and a NUL), field names (miINT8, NUL-terminated, each taking that length), then one miMATRIX per field. |
| 3 | mxOBJECT_CLASS | Class name (miINT8), then as a structure. |
| 4 | mxCHAR_CLASS | Characters in column-major order: miUINT16 when not Unicode-encoded, otherwise one of the miUTF types. |
| 5 | mxSPARSE_CLASS | Row indices ir (nzmax values) and column indices jc (columns + 1 values), both miINT32, then real and optional imaginary values. |
| 6 | mxDOUBLE_CLASS | Real part, then imaginary part if complex. The stored type may be smaller than the class: MATLAB uses the smallest of miINT32, miUINT16, miINT16 or miUINT8 that holds the data, and the class says what to convert back to. |
| 7 | mxSINGLE_CLASS | |
| 8 | mxINT8_CLASS | |
| 9 | mxUINT8_CLASS | |
| 10 | mxINT16_CLASS | |
| 11 | mxUINT16_CLASS | |
| 12 | mxINT32_CLASS | |
| 13 | mxUINT32_CLASS | |
| 14 | mxINT64_CLASS | |
| 15 | mxUINT64_CLASS |
The document lists no classes above 15.
Version 7.3
MathWorks’ versions page says -v7.3 files use an
HDF5-based format that stores data in
compressed chunks, and allow variables of 2 GB and more on 64-bit computers. By
default save writes Version 7 files, while files made with
matfile default to 7.3. The format document does not describe the
HDF5 layout.
Level 4 files
Before MATLAB 5, each matrix is a 20-byte header of five 32-bit integers, then the name and the values.
| Field | Meaning |
|---|---|
type | MOPT as a decimal number: M machine (0 little-endian IEEE, 1 big-endian IEEE, 2 VAX D-float, 3 VAX G-float, 4 Cray), O 0, P precision (0 double, 1 single, 2 int32, 3 int16, 4 uint16, 5 uint8), T type (0 numeric, 1 text, 2 sparse). |
mrows, ncols | Size. |
imagf | 1 if there is an imaginary part. |
namlen | Length of the name including its NUL. |