Create Custom Programs to Read MAT-Files - MATLAB & Simulink (original) (raw)

Main Content

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:

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