Documentation Center

  • Trial Software
  • Product Updates

Contents

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

MAT-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 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

Platform

Contents

Folder

Microsoft® Windows®

Include files

matlabroot\extern\include

Libraries

matlabroot\bin\win32 or matlabroot\bin\win64

Examples

matlabroot\extern\examples\eng_mat

UNIX®

Include files

matlabroot/extern/include

Libraries

matlabroot/bin/arch

Examples

matlabroot/extern/examples/eng_mat

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 shared (dynamically linkable) libraries for linking your programs, is platform-dependent.

Shared Libraries on Windows Systems.  The bin folder contains the shared libraries for linking your programs:

  • 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 linking your programs. 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?