Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Examples of MAT-Files

Creating a MAT-File in C

The program, matcreat.c, illustrates how to use the library routines to create a MAT-file that can be loaded 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, you can open the file in the MATLAB Editor.

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

Once you have compiled and linked your MAT-file program, you can run the stand alone application you have just produced. This program creates mattest.mat, a MAT-file that can be loaded 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 that the MAT-file was created, 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 the MATLAB Editor.

Reading a MAT-File in C

This program, matdgns.c, illustrates how to use the library routines to read and diagnose a MAT-file. To see the code, you can 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

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

Once you have compiled and linked your MAT-file program, you can run the stand alone application you have just produced. This program creates a MAT-file, matdemo.mat, that can be loaded 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 that the MAT-file has been created, 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

This program, matdemo2.F, illustrates how to use the library routines to read the MAT-file created by matdemo1.F and describe its contents. To see the code, you can open the file in the 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
  


Recommended Products

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

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