Using MAT-Files

Introduction

MAT-files, the data file format MATLAB® software uses for saving data to your disk, provide a convenient mechanism for moving data between platforms and for importing and exporting data to stand alone MATLAB applications.

To simplify your use of MAT-files in applications outside of the MATLAB environment, we have developed a library of access routines with a mat prefix that you can use in your own C or Fortran programs to read and write MAT-files. Programs that access MAT-files also use the mx prefixed API (application program interface) routines discussed in Creating C Language MEX-Files and Creating Fortran MEX-Files.

Importing Data into the MATLAB® Workspace

The best method for importing data into MATLAB depends on how much data there is, whether the data is already in machine-readable form, and what format the data is in. Here are some choices; select the one that best meets your needs.

Exporting Data from the MATLAB® Workspace

There are several methods for exporting MATLAB data.

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 M-files containing functions and scripts, and MAT-files containing binary data.

Both types of files can be transported directly between machines: M-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 M-files in ASCII file mode. Failure to set these modes correctly corrupts the data.

Reading and Writing MAT-Files

Use the save function to save MATLAB arrays currently in memory to a binary file called a MAT-file. MAT-files have the extension .mat. The load command reads MATLAB arrays from a MAT-file on disk back into the MATLAB workspace.

A MAT-file contains one or more of the data types supported in MATLAB version 5 or later, including strings, matrices, multidimensional arrays, structures, and cell arrays. MATLAB writes the data sequentially onto disk in a continuous byte stream.

MAT-File Interface Library

The MAT-file interface library contains routines for reading and writing MAT-files. You can call these routines from your own 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.

Functions in the MAT-file library begin with the three-letter prefix mat. These tables list and describe the MAT-functions.

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

matGetFp

Get an ANSI® C file pointer to 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).

Fortran 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

Get a named MATLAB array from a MAT-file.

matGetVariableInfo

Get header for named MATLAB array from a MAT-file.

matPutVariable

Put a MATLAB array into a MAT-file.

matPutVariableAsGlobal

Put a MATLAB array into a MAT-file.

matGetNextVariable

Get the next sequential MATLAB array from a MAT-file.

matGetNextVariableInfo

Get header for next sequential MATLAB array from a MAT-file.

matDeleteVariable

Remove a MATLAB array from a MAT-file.

Writing Character Data

By default, MATLAB writes character data to MAT-files using Unicode® character encoding. To override this setting and use your system's default encoding instead, do one of the following:

See the individual reference pages for these functions for more information.

You can also set a save preference for all MATLAB sessions. For more information, see MAT-Files Preferences in the "General Preferences for MATLAB" section of the Desktop Tools and Development Environment documentation.

ASCII Data Formats

When writing character data using Unicode character encoding (the default), MATLAB checks if the data is 7-bit ASCII. If it is, MATLAB writes the 7-bit ASCII character data to the MAT-file using 8 bits per character (UTF-8 format), thus minimizing the size of the resulting file. Any character data that is not 7-bit ASCII is written in 16-bit Unicode form (UTF-16). This algorithm operates on a per-string basis.

Converting Character Data

Writing character data to MAT-files using Unicode character encoding enables you to share data with users that have systems with a different default system character encoding scheme, without character data loss or corruption. Although conversion between Unicode encoding and other encoding forms is often lossless, there are scenarios in which round-trip conversions can result in loss of data. The following guidelines may reduce your chances of data loss or corruption.

In order to prevent loss or corruption of character data, all users sharing the data must have at least one of the following in common:

For example, if one user on a Japanese language operating system writes ASCII data having more than 7 bits per character to a MAT-file, another user running MATLAB version 6.5 on an English language operating system will be unable to read the data accurately. However, if both have MATLAB version 7, the information can be shared without corruption or loss of data.

Finding Associated Files

A collection of files associated with reading and writing MAT-files is located on your disk. The following table, MAT-Function Subdirectories, lists the path to the required subdirectories for importing and exporting data using MAT-functions. The term matlabroot refers to the root directory of your MATLAB installation.

MAT-Function Subdirectories

Platform

Contents

Directories

Microsoft® Windows®

Include files

matlabroot\extern\include

Libraries

matlabroot\bin\win32 or matlabroot\bin\win64

Examples

matlabroot\extern\examples\eng_mat

UNIX®[a]

Include files

matlabroot/extern/include

Libraries

matlabroot/bin/$arch

Examples

matlabroot/extern/examples/eng_mat

[a] UNIX is a registered trademark of The Open Group in the United States and other countries.

Include Files

The include subdirectory 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 subdirectory contains:

Libraries

The libraries subdirectory, that contains shared (dynamically linkable) libraries for linking your programs, is platform dependent.

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

Shared Libraries on UNIX® Systems.   The bin/$arch subdirectory, where $arch is your machine's architecture, contains the shared libraries for linking your programs. For example, on Sun™ Solaris™ systems, the subdirectory is bin/sol64:

Example Files

The examples/eng_mat subdirectory contains C and Fortran source code for a number of example files that demonstrate how to use the MAT-file routines. The source code files are the same for both Windows and UNIX systems.

C and Fortran Examples

Library

Description

matcreat.c

C program that demonstrates how to use the library routines to create a MAT-file that can be loaded into MATLAB

matcreat.cpp

C++ version of the matcreat.c program

matdgns.c

C program that demonstrates how to use the library routines to read and diagnose a MAT-file

matdemo1.F

Fortran program that demonstrates how to call the MATLAB MAT-file functions from a Fortran program

matdemo2.F

Fortran program that demonstrates how to use the library routines to read the MAT-file created by matdemo1.F and describe its contents

For more information about MATLAB API files and directories, see Additional Information.


[a] UNIX is a registered trademark of The Open Group in the United States and other countries.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS