Main Content

Create Custom Programs to Read MAT-Files

Why Write Custom Applications?

To bring data into a MATLAB® application, see Supported File Formats for Import and Export. To save data to a MAT-file, see Save and Load 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 API 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. For more information, see MAT-File API Library and Include Files.

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

MAT-File Routines

MAT-File API Function

Purpose

matOpen

Open a MAT-file.

matClose

Close a MAT-file.

matGetDir

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

matGetVariable

Read a MATLAB array from a MAT-file.

matPutVariable

Write a MATLAB array to a MAT-file.

matGetNextVariable

Read the next MATLAB array from a MAT-file.

matDeleteVariable

Remove a MATLAB array from a MAT-file.

matPutVariableAsGlobal

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

matGetVariableInfo

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

matGetNextVariableInfo

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

MAT-File C-Only Routines

matGetFp

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 API functions. MATLAB libraries are not multithread safe so you can use these functions only on a single thread at a time.

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.