Skip to content

What is a .mat file?

By MegaRows · Published · Updated · 4 min read

A .mat file holds saved MATLAB variables

MathWorks’ documentation describes MAT files as binary MATLAB files that store workspace variables, and its table of file formats calls the type a “Saved MATLAB workspace”. MATLAB’s save command writes one. Called on its own, it puts every variable of the current workspace into a file named matlab.mat, replacing a file of that name if there is one. load brings the variables back, all of them or only those asked for by name.

The extension matters to MATLAB: load treats a file whose name ends in anything other than .mat as text, unless it is told otherwise with -mat.

What is inside a .mat file

Variables, each kept under its own name. Depending on the version, they can be two-dimensional or N-dimensional arrays, character and sparse arrays, cell arrays and structure arrays. In MATLAB, whos -file lists the variables of a file with their sizes and types.

A whole file need not be read at once. A MAT file object, made with matfile, reads and changes variables in the file without loading them into memory; doing that efficiently needs a version 7.3 file. And when a variable needs something the chosen version lacks, save leaves that item out and gives a warning, so an older version can hold less than the workspace did.

MAT-file versions 4, 6, 7 and 7.3 compared

There are four versions, and MATLAB R2006b and every later release supports all of them.

MAT-file versions 4, 6, 7 and 7.3: supporting releases, what each added, compression and the largest variable
VersionMATLAB releasesWhat it addedCompressedLargest variable
Version 4All releasesTwo-dimensional double, character and sparse arraysNo100,000,000 elements per array, and 2³¹ bytes per variable
Version 6MATLAB 5 (R8) and laterN-dimensional arrays, cell arrays and structure arrays; variable names longer than 19 charactersNo2³¹ bytes per variable
Version 7MATLAB 7.0 (R14) and laterUnicode character encoding, so files can be shared between systems; compressionYes2³¹ bytes per variable
Version 7.3R2006b and laterSaving and loading parts of a variableYes2 GB or more, on 64-bit computers
  • The default is version 7. A plain save writes it. The default can be changed in MATLAB’s settings (Home › Environment › Settings › MATLAB › General › MAT and FIG Files), or set for one file with a flag such as -v7.3. A new file created through matfile is version 7.3.
  • Compression starts with version 7. From that version on, MATLAB compresses data as it writes, to save storage space. The -nocompression option of save turns it off, for version 7 and 7.3 files only.
  • Version 7.3 is built on HDF5. It stores data in compressed chunks, and the format needs some storage of its own, so a version 7.3 file of cell arrays or structures is sometimes larger than the same data saved as version 7.
  • The limits are per variable. The size of the file as a whole is limited only by the computer’s file system.

To see the four versions side by side, the sample .mat files to download hold the same made-up weather saved as version 4, 6, 7 and 7.3.

.mat and .m: data and code

The two extensions look alike and mean opposite things. A .m file is MATLAB code written as text, a script or a function, which is read in an editor and run. A .mat file is data in binary form, which is loaded. A project often has both: the code that made the results, and the results.

Other file types that use .mat

The extension is not MATLAB’s alone. Microsoft Access uses it for table shortcuts, and 3ds Max and V-Ray use it for materials. Those files have nothing to do with the format on this page, and the MegaRows tools do not read them.

Open a .mat file without MATLAB

The MAT file viewer reads MATLAB data files of versions 4 to 7.3 in your browser, lists their variables and plots the signals. The file is read on your device and is not uploaded. Some MATLAB objects are listed but not read: the viewer’s page says what isn’t supported yet.

For a spreadsheet or another program, MAT to CSV saves the signals you choose as a CSV file. The guide to opening a .mat file without MATLAB compares the other routes, such as Python and GNU Octave, and CSV to MAT goes the other way.

Frequently asked questions

What is the difference between a .m file and a .mat file?

A .m file holds MATLAB code as text, a script or a function. A .mat file holds saved data in binary form. Code is run; data is loaded.

Which version does MATLAB save by default?

Version 7, unless the default is changed in MATLAB’s settings or a version flag such as -v7.3 is given to save. A new file created through matfile is version 7.3.

How large can a .mat file be?

The limits are per variable: 2³¹ bytes in versions 4, 6 and 7, and 2 GB or more in version 7.3 on 64-bit computers. The file as a whole is limited only by the computer’s file system.

Is every .mat file a MATLAB file?

No. Microsoft Access, 3ds Max and V-Ray use the same extension for files of their own, which hold no MATLAB data. The MegaRows tools read MATLAB data files only.

Is it safe to load a .mat file from an unknown source?

MathWorks’ documentation for load cautions that the command might execute code contained in a MAT file as it initialises variables, so a file from a source you do not know calls for care in MATLAB.

Can I open a .mat file without MATLAB?

Yes. The MAT file viewer reads versions 4 to 7.3 in your browser, with the limits its page lists, and the MAT to CSV converter saves the signals you choose as a CSV.

Sources

The statements about MATLAB’s format come from these pages of MathWorks’ MATLAB documentation, read on .