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, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import 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
Options
- Import options:
-
"IndeterminateValues" Automatic replacement values for infinity, NaN, etc. - Use "IndeterminateValues" to provide replacement rules for different values. The following settings are supported:
-
val a fixed value val for any indeterminate value <|"NaN"…,"Inf"…,"-Inf"…|> specific replacements depending on the type indeterminate - Export options:
-
CompressionLevel Automatic compression level to use OverwriteTarget True whether to overwrite an existing file "Version" Automatic version 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.
Examples
open all close allBasic Examples (3)
Scope (9)
Import (2)
List available Import elements
Import["ExampleData/numbers.mat", "Elements"]Import a variable with label "y":
Import["ExampleData/numbers.mat", "y"]Export (7)
Export an array of numbers to a MAT-file:
Export["file.mat", Range[5]]If no variable name is given, the default variable name "Expression1" is used:
Import["file.mat", "LabeledData"]Export data with custom variable name:
Export["file.mat", "v" -> Range[10]]Import[%, "LabeledData"]Export multiple variables to a file. Variable names must be explicitly given:
Export["file.mat", {"v1" -> Range[10], "v2" -> Reverse@Range[5]}]Import[%, "LabeledData"]Export a SparseArray:
Export["file.mat", SparseArray[{{1, 1} -> 1., {2, 2} -> 2., {3, 3} -> 3., {1, 4} -> 4.}]]Import[%, "Data"]Export["file.mat", {{"a", "bb"}, {"ccc", "dddd"}}]Import[%, "Data"]Export an array of expressions of possibly different types:
Export["file.mat", {{"a", 5}, {{Pi, E}, "text"}}]Import[%, "Data"]Export an array of associations with consistent keys into a "structure" array:
data = Table[<|"a" -> 10 * i + j, "b" -> IntegerName[j * j + i]|>, {i, 1, 3}, {j, 1, 2}];data//GridExport["file.mat", data]Import[%, "Data"]//First//GridImport Elements (10)
"Comments" (1)
"Data" (2)
"DataFormat" (1)
"Dimensions" (1)
"LabeledData" (2)
Import data as a list or namedata rules, for all variables in the file:
Import["ExampleData/numbers.mat", "LabeledData"]Create a file with two variables:
Export["file.mat", {"v1" -> Range[10], "v2" -> Reverse@Range[5]}];Import["file.mat", {"LabeledData", "v1"}]Import variables whose name matches a string pattern:
Import["file.mat", {"LabeledData", "v*"}]"Labels" (1)
"Summary" (1)
Export Options (14)
CompressionLevel (3)
Export a large numeric array. By default, the data will be compressed (equivalent to level 1):
Export["file.mat", RandomInteger[1000, {1000, 1000}]]FileSize["file.mat"]Export data with no compression:
Export["file.mat", RandomInteger[1000, {1000, 1000}], CompressionLevel -> 0]FileSize["file.mat"]Compression is not supported in MAT version 4:
Export["file.mat", RandomInteger[1000, {1000, 1000}], "Version" -> 4, CompressionLevel -> 1]FileSize["file.mat"]"Header" (3)
By default, an automatic header is created:
Export["file.mat", {1, 2, 3}, "Header" -> Automatic]Import["file.mat", "Comments"]Specify a header, which is stored after the MAT version number:
Export["file.mat", {1, 2, 3}, "Header" -> "I created this file"]The header can be imported using the "Comments" element:
Import["file.mat", "Comments"]The header cannot be too long:
Export["file.mat", {1, 2, 3}, "Header" -> StringJoin@@Table[Alphabet[], 5]];OverwriteTarget (3)
By default, a new file is created for each call to Export:
Export["file.mat", {1, 2, 3}]With OverwriteTargetFalse, existing files will not be overwritten:
Export["file.mat", {4, 5, 6}, OverwriteTarget -> False]To append variables at the end of an existing file, use OverwriteTarget"Append":
Export["file.mat", {"v" -> {4, 5, 6}}, OverwriteTarget -> "Append"]Import["file.mat", "LabeledData"]A variable being appended to an existing file should have a new name:
Export["file.mat", {"v" -> {Pi, Pi}}, OverwriteTarget -> "Append"]"Version" (5)
By default, Export creates MAT-files Version 5:
Export["file.mat", {1, 2, 3}]Import[%, "Version"]Export["file.mat", {1, 2, 3}, "Version" -> 4]Import[%, "Version"]Export data to MAT Version 7.3:
Export["file.mat", {1, 2, 3}, "Version" -> 7.3]Import[%, "Version"]Version can be specified either as a number or a string:
Export["file.mat", {1, 2, 3}, "Version" -> "7.3"]Import[%, "Version"]Some data types (e.g. arrays of mixed types) cannot be exported to MAT Version 4:
Export["file.mat", {1, "text", Pi}, "Version" -> "4"]See Also
Import Export CloudExport CloudImport
Formats: HDF5 NetCDF NASACDF SAS7BDAT XPORT MTP MTX HarwellBoeing
History
Introduced in 2004 (5.1) | Updated in 2010 (8.0) ▪ 2020 (12.2) ▪ 2021 (12.3)