Writing Custom Applications to Read and Write MAT-Files

Why Write Custom Applications?

To bring data into a MATLAB® application, see Methods for Importing Data. To save data to a MAT-file, see Save, Load, and Delete Workspace Variables. Use these procedures when you program your entire application in MATLAB, or if you share data with other MATLAB users. There are situations, however, when you must write a custom program to interact with data. For example:

  • Your data has a custom format.

  • You create applications for users who do not run MATLAB, and you want to provide them with MATLAB data.

  • You want to read data from an external application, but you do not have access to the source code.

Before writing a custom application, determine if MATLAB meets your data exchange needs by reviewing the following topics:

If these features are not sufficient, you can create custom C/C++ or Fortran programs to read and write data files in the format required by your application. There are two types of custom programs:

MAT-File Interface Library

The MAT-File Library contains routines for reading and writing MAT-files. Call these routines from your own C/C++ and Fortran programs. Use these routines, rather than attempt to write your own code, to perform these operations, since using the library insulates your applications from future changes to the MAT-file structure.

MATLAB provides the MATFile type for representing a MAT-file.

MAT-File Routines




Open a MAT-file.


Close a MAT-file.


Get a list of MATLAB arrays from a MAT-file.


Read a MATLAB array from a MAT-file.


Write a MATLAB array to a MAT-file.


Read the next MATLAB array from a MAT-file.


Remove a MATLAB array from a MAT-file.


Put a MATLAB array into a MAT-file such that the load command places it into the global workspace.


Load a MATLAB array header from a MAT-file (no data).


Load the next MATLAB array header from a MAT-file (no data).

MAT-File C-Only Routines


Get an ANSI® C file pointer to a MAT-file.

The MAT-File Interface Library does not support MATLAB objects created by user-defined classes.

Do not create different MATLAB sessions on different threads using MAT-File Library functions. MATLAB libraries are not multithread safe so you can use these functions only on a single thread at a time.

Finding Associated Files

MATLAB provides the include and library files needed to write programs to read and write MAT-files. The following table lists the path names to these files. The term matlabroot refers to the root folder of your MATLAB installation. The term arch is a unique string identifying the platform.

MAT-Function Folders




Microsoft® Windows®

Include files



matlabroot\bin\win32 or matlabroot\bin\win64




Include files






MAT-Function Include Files

The include folder holds header files containing function declarations with prototypes for the routines that you can access in the API Library. These files are the same for both Windows and UNIX systems. The folder contains:

  • The matrix.h header file that contains a definition of the mxArray structure and function prototypes for matrix access routines.

  • The mat.h header file that contains function prototypes for mat routines.

MAT-Function Libraries

The name of the libraries folder, which contains the shared (dynamically linkable) libraries, is platform-dependent.

Shared Libraries on Windows Systems.  The bin folder contains the run-time version of the shared libraries:

  • The libmat.dll library of MAT-file routines (C/C++ and Fortran)

  • The libmx.dll library of array access and creation routines

Shared Libraries on UNIX Systems.  The bin/arch folder, where arch is your machine's architecture, contains the shared libraries. For example, on Apple Macintosh 64-bit systems, the folder is bin/maci64:

  • The libmat.dylib library of MAT-file routines (C/C++ and Fortran)

  • The libmx.dylib library of array access and creation routines

Example Files

The examples/eng_mat folder contains C/C++ and Fortran source code for examples demonstrating how to use the MAT-file routines.

Exchanging Data Files Between Platforms

You can work with MATLAB software on different computer systems and send MATLAB applications to users on other systems. MATLAB applications consist of MATLAB code containing functions and scripts, and MAT-files containing binary data.

Both types of files can be transported directly between machines: MATLAB source files because they are platform independent, and MAT-files because they contain a machine signature in the file header. MATLAB checks the signature when it loads a file and, if a signature indicates that a file is foreign, performs the necessary conversion.

Using MATLAB across different machine architectures requires a facility for exchanging both binary and ASCII data between the machines. Examples of this type of facility include FTP, NFS, and Kermit. When using these programs, be careful to transmit MAT-files in binary file mode and MATLAB source files in ASCII file mode. Failure to set these modes correctly corrupts the data.

Was this topic helpful?