MAT (.mat)

Background & Context

    • MATLAB MAT-files.
    • Matrix data format.
    • Native data format of the MATLAB numerical computation software.
    • Stores numerical matrices, Boolean values, or strings.
    • Also stores sparse arrays, nested structures and more.
    • Binary format.
    • Version 7.3 of MAT-files uses HDF5.

Import & Export

  • Import["file.mat"] imports a MAT-file, returning a multidimensional array containing numbers, strings or Boolean values.
  • Import["file.mat",elem] imports the specified element from a MAT-file.
  • The import format can be specified with Import["file", "MAT"] or Import["file",{"MAT",elem,}].
  • Export["file.mat",expr] creates a MAT-file from an array of integers, doubles and complex values.
  • Export["file.mat",{elem1->expr1,elem2->expr2,},"Rules"] uses rules to specify the elements to be exported.
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Data representation element:
  • "Data"numerical or textual data given as a multidimensional array
    "LabeledData"data given as a list of rules
  • Import and Export use the "Data" element by default.
  • Data descriptor elements:
  • "DataFormat"variable data type
    "Dimensions"dimensions of the array
  • When appropriate, numerical data is imported as SparseArray objects.
  • Meta-information element:
  • "Comments"user comments stored in the file
    "Labels"variable names given as a list of strings
    "Version"version of the MAT specification for the file


  • Import options:
  • "IndeterminateValues"Automaticreplacement values for infinity, NaN, etc.
  • Use "IndeterminateValues" to provide replacement rules for different values. The following settings are supported:
  • vala fixed value val for any indeterminate value
    <|"Nan","Inf","-Inf"|>specific replacements depending on the type indeterminate
  • Export options:
  • CompressionLevelAutomaticcompression level to use
    OverwriteTargetTruewhether to overwrite an existing file
    "Version"Automaticversion of the file format to use
  • Using OverwriteTarget->"Append", new objects may be added to an existing file.
  • Supported versions in export are "4", "5", "7.3".
  • By default, export generates version "5" MAT-files. For variables larger than bytes, a version "7.3" file is generated.


open allclose all

Basic Examples  (3)

This imports a simple MAT-file:

Summary of a MAT-file:

Export a numerical matrix:

Scope  (8)

Import  (1)

List available Import elements

Export  (7)

Export an array of numbers to a MAT-file:

If no variable name is given, the default variable name "Expression1" is used:

Export data with custom variable name:

Export multiple variables to a file. Variable names must be explicitly given:

Export a SparseArray:

Export an array of strings:

Export an array of expressions of possibly different types:

Export an array of associations with consistent keys into a "structure" array:

Export Options  (14)

CompressionLevel  (3)

Export a large numeric array. By default, the data will be compressed (equivalent to level 1):

Export data with no compression:

Compression is not supported in MAT version 4:

"Header"  (3)

By default, an automatic header is created:

Specify a header, which is stored after the MAT version number:

The header can be imported using the "Comments" element:

The header cannot be too long:

OverwriteTarget  (3)

By default, a new file is created for each call to Export:

With OverwriteTargetFalse, existing files will not be overwritten:

To append variables at the end of an existing file, use OverwriteTarget"Append":

A variable being appended to an existing file should have a new name:

"Version"  (5)

By default, Export creates MAT-files Version 5:

Export data to MAT Version 4:

Export data to MAT Version 7.3:

Version can be specified either as a number or a string:

Some data types (e.g. arrays of mixed types) cannot be exported to MAT Version 4: