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 |
Alignment (MathWorks' document): all data that is not compressed is aligned on
64-bit boundaries, so a writer pads a short element to the next 8 bytes and a reader
allows for it. The padding is counted in the byte count of an miMATRIX
element, not of the others. The rule is for data that is not compressed: a compressed
variable is stored complete with its tags and data, in the same format as an
uncompressed one, inside a buffer after an 8-byte miCOMPRESSED tag whose
byte count is the compressed length. Every variable has a header of at least 56 bytes, even
when its data is small enough to be stored in the tag.
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; the rest of this section comes from the source of the open-source library
matio (src/mat73.c, GitHub tbeu/matio, master, read
2026-11-13), which reads and writes MATLAB-compatible v7.3 files. It is a
reimplementation, not MathWorks documentation.
- File start. The HDF5 file has a 512-byte user block (
H5Pset_userblock(512)) in front of the HDF5 superblock. The first 128 bytes are the usual MAT header: 116 bytes of text padded with spaces (MATLAB 7.3 MAT-file, Platform: ..., Created by: ...), an 8-byte subsystem data offset (spaces in this writer), the version0200(as the 16-bit value 0x0200) and the endian markerIM(0x4D49). The matio writer also limits the library version range withH5Pset_libver_bounds(earliest, V18)and names its HDF5 layout “HDF5 schema 0.5” in the text. - Variables. Each top-level variable is an HDF5 dataset or, for structs and sparse matrices, a group, named with the variable name. Its attributes describe it:
MATLAB_classis a scalar fixed-length, NUL-terminated string (variable-length strings are rejected) naming the class:cell,struct,object,char,sparse,double,single,int8,uint8,int16,uint16,int32,uint32,int64,uint64,function, orlogical.MATLAB_global(integer) marks a global variable.MATLAB_int_decode(integer) tells how a stored integer type stands for text or logicals: forchar, 1 means the stored data is uint8, 2 uint16 (UTF-16 code units, the default if absent) and 4 uint32;logicalvariables are stored as uint8 datasets withMATLAB_int_decode= 1 and classlogical.MATLAB_empty(unsigned integer, 1) marks an empty array. - Numeric arrays are datasets of the native integer or float type for the class (
doubleas 64-bit float,singleas 32-bit float,int8touint64as the matching integers). Complex arrays use a compound datatype with two members,realthenimag, each of the base type, the second at offset equal to the size of the first. The dimensions are reversed: an array of MATLAB size M×N×P is a dataset whose dataspace has dimensions P, N, M (column-major order seen as row-major), so reading the HDF5 dims backwards gives the MATLAB size; scalars may be rank 0. - Empty arrays. An empty array is a one-dimensional dataset of unsigned integers holding the MATLAB dimensions themselves (one element per dimension, for example 0 and 3), with
MATLAB_empty= 1; its length is the number of MATLAB dimensions, and its values (not its dataspace) give the size. Empty char, struct, logical and cell arrays carry the same attribute plus their ownMATLAB_class(andMATLAB_int_decode= 1 for logical,MATLAB_fieldsfor struct). - Sparse matrices are groups with the
MATLAB_sparseattribute (a 64-bit unsigned integer: the number of rows) andMATLAB_class(double,logical, …;MATLAB_int_decode= 1 if logical) holding three 1-D datasets in compressed-sparse-column form:data(the non-zero values; compoundreal/imagif complex),ir(zero-based row index of each value) andjc(column pointers: the dataspace size is columns + 1, so the column count is the size ofjcminus 1, and column c holds entriesjc[c]up tojc[c+1]).dataandirare absent when there are no non-zero entries. - Cell arrays are datasets of HDF5 object references (
H5T_STD_REF_OBJ), one per element in the same reversed layout, each pointing to a dataset or group under the file's/#refs#group, whose members are named by consecutive numbers (/#refs#/0,/#refs#/1, …). Each referenced object is a complete variable with its ownMATLAB_classand attributes; nested cells and structs recurse into/#refs#. The#refs#group is a creation-property-tuned group for many members and is not itself a variable;#subsystem#(see objects below) is also not a user variable. - Structs are groups with
MATLAB_class=struct. The field names are given by the attributeMATLAB_fields, an array of variable-length strings (a one-dimensional dataspace, one name per field, stored as HDF5 variable-length sequences of 1-byte characters); if the list is long, the attribute is instead an object reference to a dataset holding the names. If the attribute is absent the members of the group are the fields. Each field is a member of the group named for the field. For a 1×1 struct the field members hold the values directly (a field dataset that hasMATLAB_classis the value itself); for a struct array each field is a dataset of object references (one per element) into/#refs#, and the dimensions of that reference dataset are the dimensions of the struct array. - Objects. Instances of MATLAB classes (the MCOS object system) are integer datasets whose
MATLAB_classattribute is the class name (not one of the names above); a code comment in matio calls them uint8 datasets while the code reads their contents asuint32reference values. These values are metadata (object ids and class ids) that point into the#subsystem#group of the file, where the property values are stored (matio resolves them with itsMat_MCOS_Read73from the “FileWrapper__” cell array). A group with an unknown class name (for example an enumeration instance) is treated as a struct-like container. The detailed layout of theuint32metadata and of the subsystem data was not read for this page (it is in matio'smat.candmat73.cMCOS code) and is not documented by MathWorks. - Compression and chunking. MathWorks says v7.3 stores data in compressed chunks; matio's reader recognises the HDF5 deflate filter (
H5Z_FILTER_DEFLATE) on a dataset as zlib compression and picks its own chunk sizes when writing. The filter pipeline itself is ordinary HDF5.
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. |
(MathWorks' document, taken from the MATLAB V4.2 External Interface Guide.) A file
is a plain sequence of matrices, one after another, each starting with this
20-byte header. Then comes the name (namlen bytes, the last a NUL), the
real part (mrows × ncols numbers in the precision P
gives, stored column by column), and, when imagf is non-zero, the
imaginary part in the same form. Text matrices (T = 1) store each character
as a floating-point number between 0 and 255 (an ASCII code). Only two-dimensional
matrices and character strings can be stored.
Precision chosen by save: any matrix with a non-integer entry, or with
10,000 or fewer elements, is saved as double (8 bytes per real element). A matrix of
integers with more than 10,000 elements is saved with 1 byte per element if all
values are in 0..255, 2 bytes if in 0..65535 or −32767..32767, 4 bytes if in
−231+1..231−1, otherwise 8.
Level 4 sparse matrices (T = 2). MathWorks' document gives
T = 2 as a sparse matrix but no layout. The open-source library matio
(src/mat4.c, read 2026-11-13, both its reader and writer) stores a sparse
matrix as an ordinary Level 4 matrix of doubles in coordinate form with
mrows = (number of stored entries) + 1 and ncols = 3 for a real
matrix or 4 for a complex one (the reader tests ncols == 4 for complex;
imagf is written 0). The columns, each of mrows numbers, are: the
one-based row index of each entry, with the last element equal to the number of rows of
the sparse matrix; the one-based column index of each entry (non-decreasing), with the last
element equal to the number of columns; the real parts of the values, with a final 0; and,
if complex, the imaginary parts, with a final 0. Entries are in column-major order. The
reader rejects a row index of 0.
Machine formats. The same library accepts only M = 0 (IEEE
little-endian) and 1 (IEEE big-endian) (it checks MOPT against 4052, byte-swaps
if needed, requires O = 0, and for M other than 0 and 1, “VAX, Cray, or bogus”,
gives up); the document names the VAX D-float, VAX G-float and Cray formats
(M = 2, 3, 4) without a layout, and no public decoder for them was found. The
document strongly advises using MathWorks' MAT-file library rather than
writing the format by hand. The example in the document writes type 1000, 1 × 1,
imagf 1, namlen 2 for a variable named x: a
little-endian double matrix with real part 1.0 and imaginary part 2.0.