Skip to Main Content Skip to Search
Product Documentation

Examples of MAT-File Applications

List of Examples

The matlabroot/examples/eng_mat folder contains C/C++ and Fortran source code for examples demonstrating how to use the MAT-file routines. These examples create standalone programs. The source code is the same for both Windows and UNIX systems.

Example

Description

matcreat.c

C program that demonstrates how to use the library routines to create a MAT-file that you can load 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.

matimport.c

C program based on matcreat.c used in the example for writing standalone applications.

matreadstructarray.c

C program based on explore.c to read contents of a structure array.

matreadcellarray.c

C program based on explore.c to read contents of a cell array.

For tips about how to modify other MATLAB examples to work with an mxArray in your application, see Examples for Working with mxArrays.

Creating a MAT-File in C

The matcreat.c example illustrates how to use the library routines to create a MAT-file that you can load into the MATLAB workspace. The program also demonstrates how to check the return values of MAT-function calls for read or write failures. To see the code, open the file in MATLAB Editor.

To produce an executable version of this program, compile the file and link it with the appropriate library. For details on platform specifics, see Compiling and Linking MAT-File Programs.

After compiling and linking your MAT-file program, you can run the standalone application you just produced. This program creates mattest.mat, a MAT-file that you can load into MATLAB. To run the application, depending on your platform, either double-click its icon or enter matcreat at the system prompt:

matcreat
Creating file mattest.mat...

To verify the MAT-file, at the command prompt, type:

whos -file mattest.mat
  Name               Size         Bytes  Class

  GlobalDouble       3x3             72  double array (global)
  LocalDouble        3x3             72  double array
  LocalString        1x43            86  char array

Grand total is 61 elements using 230 bytes

Creating a MAT-File in C++

There is a C++ version of matcreat.c in the matlabroot\extern\examples\eng_mat folder. To see matcreat.cpp, open the file in MATLAB Editor.

Reading a MAT-File in C/C++

The matdgns.c example illustrates how to use the library routines to read and diagnose a MAT-file. To see the code, open the file in MATLAB Editor.

After compiling and linking this program, you can view its results:

matdgns mattest.mat
Reading file mattest.mat...
 
Directory of mattest.mat:
GlobalDouble
LocalString
LocalDouble
 
Examining the header for each variable:
According to its header, array GlobalDouble has 2 dimensions
  and was a global variable when saved
According to its header, array LocalString has 2 dimensions
  and was a local variable when saved
According to its header, array LocalDouble has 2 dimensions
  and was a local variable when saved
 
Reading in the actual array contents:
According to its contents, array GlobalDouble has 2 dimensions
  and was a global variable when saved
According to its contents, array LocalString has 2 dimensions
  and was a local variable when saved
According to its contents, array LocalDouble has 2 dimensions
  and was a local variable when saved
Done

Creating a MAT-File in Fortran

The matdemo1.F example creates the MAT-file, matdemo.mat. To see the code, you can open the file in MATLAB Editor.

After compiling and linking your MAT-file program, you can run the standalone application you just produced. This program creates a MAT-file, matdemo.mat, that you can load into MATLAB. To run the application, depending on your platform, either double-click its icon or enter matdemo1 at the system prompt:

matdemo1
Creating MAT-file matdemo.mat ...
Done creating MAT-file

To verify the MAT-file, at the command prompt, enter:

 whos -file matdemo.mat
 Name          Size         Bytes  Class
 
 Numeric       3x3             72  double array
 String        1x33            66  char array
 
Grand total is 42 elements using 138 bytes

Reading a MAT-File in Fortran

The matdemo2.F example illustrates how to use the library routines to read the MAT-file created by matdemo1.F and describe its contents. To see the code, open the file in MATLAB Editor.

After compiling and linking this program, you can view its results:

 matdemo2
 Directory of Mat-file:
 String                          
 Numeric                         
 Getting full array contents:
   1
 Retrieved String                          
   With size   1-by-  33
   3
 Retrieved Numeric                         
   With size   3-by-  3

Examples for Working with mxArrays

The MAT-File Interface Library lets you access MATLAB arrays (type mxArray) in a MAT-file. To work directly with an mxArray in a C/C++ or Fortran application, use functions in the MX Matrix Library. The options files already link to this library, as described in What You Need.

You can find examples for working with mxArrays in the matlabroot/extern/examples/mex and matlabroot/extern/examples/mx folders. The following topics show C code examples, based on these MEX examples, for working with cells and structures. The examples show how to read cell and structure arrays and display information based on the type of the mxArray within each array element.

If you create an application from one of the MEX examples, here are some tips for adapting the code to a standalone application.

Read Structures from a MAT-File

The matreadstructarray.c example is based on the analyze_structure function in explore.c. For simplicity this example only processes real elements of type double; refer to the explore.c example for error checking and processing other types.

 matreadstructarray.c

After compiling and linking your MAT-file program, you can run the standalone application on the following MAT-file, testpatient.mat. Create a structure, patient, and save it:

patient(1).name = 'John Doe';
patient(1).billing = 127.00;
patient(1).test = [79 75 73; 180 178 177.5; 172 170 169];
patient(2).name = 'Ann Lane';
patient(2).billing = 28.50;
patient(2).test = [68 70 68; 118 118 119; 172 170 169];

save('testpatient.mat')

To calculate the total of the billing field, type:

!matreadstruct testpatient.mat patient billing
Total for billing: 155.50 

Read Cell Arrays from a MAT-File

The matreadcellarray.c example is based on the analyze_cell function in explore.c.

 matreadcellarray.c

After compiling and linking your MAT-file program, you can run the standalone application on the following MAT-file, testcells.mat. Create 3 cell variables and save:

cellvar = {'hello'; [2 3 4 6 8 9]; [2; 4; 5]};
structvar = {'cell with a structure'; patient; [2; 4; 5]};
multicellvar = {'cell with a cell'; cellvar; patient};
save('testcells.mat','cellvar','structvar','multicellvar')

To display the mxArray type for the contents of cell cellvar, type:

!matreadcell testcells.mat cellvar
0: string 
1: numeric class 
2: numeric class
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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