MatIO

From NA-Wiki

Revision as of 09:18, 8 August 2007 by Dag (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

MatIO is a powerful library that provides facilites for writing and reading Matlab .mat files from C or Fortran, written by Christopher Hulbert (download it here). This is nice if one uses Matlab for plotting or post-processing of data from some program written in C. It can write and read the compund data structures that Matlab uses, cells and structs.

Grab the example code here]

There is also a Minimal interface to matIO in C available.

Contents

Simple example: Writing a vector

#include <matio.h>

int main(void)
{

  const double a[5] = {43.5, 5432.434, 3.32, 0.0001, 88834.0};

  mat_t *mat;
  matvar_t *matvar;
  int dims[2] = {1,5};

  mat = Mat_Open("simple.mat",MAT_ACC_RDWR);

  if(mat)
    {
      matvar = Mat_VarCreate("vec1",MAT_C_DOUBLE,MAT_T_DOUBLE,2,dims,a,0);
      Mat_VarWrite( mat, matvar, 0);

      Mat_VarFree(matvar);
      Mat_Close(mat);
    }
  else
    return 1;
    
  return 0;
}

Compile this program with

$ gcc simple.c -lmatio -lz

This program will create a file simple.mat. In matlab, doing load simple.mat creates a variable named vec1 (name defined in the Mat_VarCreate-statement). Note that the variable dims contains the dimension of the vector to be written.

Another example

This example will write three different matrices to a single file.

/*
  Simple MatIO demo. 
  Writes three matrices to the same .mat-file

  Dag Lindbo, 2007-06-19
*/

#include <matio.h>

int main(void)
{
  int i,j;
  const int m1 = 20, n1 = 40, m2 = 200, n2 = 923, m3 = 678, n3 = 923;

  /* create some matrices*/
  double mat1[m1][n1];
  double mat2[m2][n2];
  double mat3[m3][n3];

  for(i=0;i<m1;i++)
    for(j=0;j<n1;j++)
      mat1[i][j] = (i+j)*3.14;
  
  for(i=0;i<m2;i++)
    for(j=0;j<n2;j++)
      mat2[i][j] = (i+j)*7.28;

  for(i=0;i<m3;i++)
    for(j=0;j<n3;j++)
      mat3[i][j] = (i+j)*2.81;

  /* setup the output */
  mat_t *mat;
  matvar_t *matvar;
  int dims1[2] = {m1,n1};
  int dims2[2] = {m2,n2};
  int dims3[2] = {m3,n3};
  mat = Mat_Open("three_matrix.mat",MAT_ACC_RDWR);

  if(mat)
    {
      /* first matrix */
      matvar = Mat_VarCreate("mat1",MAT_C_DOUBLE,MAT_T_DOUBLE,2,
			     dims1,mat1,0);
      Mat_VarWrite( mat, matvar, 0);
      Mat_VarFree(matvar);

      /* secon matrix */
      matvar = Mat_VarCreate("mat2",MAT_C_DOUBLE,MAT_T_DOUBLE,2,
			     dims2,mat2,0);
      Mat_VarWrite( mat, matvar, 0);
      Mat_VarFree(matvar);

      /* third matrix */
      matvar = Mat_VarCreate("mat3",MAT_C_DOUBLE,MAT_T_DOUBLE,2,
			     dims3,mat3,0);
      Mat_VarWrite( mat, matvar, 0);
      Mat_VarFree(matvar);

      Mat_Close(mat);
    }
  else
    return 1;
    
  return 0;
}

Now, in Matlab do

>> load three_matrix.mat
>> whos

and you see

  Name        Size               Bytes  Class     Attributes

  mat1       20x40                6400  double
  mat2      200x923            1476800  double
  mat3      678x923            5006352  double

Writing a struct

#include <matio.h>

int main(void)
{

  const double a[5] = {43.5, 5432.434, 3.32, 0.0001, 88834.0};
  const double b[4] = {43.5, 5432.434, 3.32, 0.0001};
  const double c[1] = {43};
  
  mat_t *mat;
  matvar_t **matvar, *struct_matvar;
  int dims1[2] = {1,5};
  int dims2[2] = {4,1};
  int dims3[2] = {1,1};
  int dims_struct[2] = {1,1}; 

  mat = Mat_Create("struct_out.mat",NULL);

  if(mat)
    {
      matvar = malloc(4*sizeof(matvar_t *));
      matvar[0] = Mat_VarCreate("vec1",MAT_C_DOUBLE,MAT_T_DOUBLE,2,
				dims1,(void*)a,0);
      matvar[1] = Mat_VarCreate("vec2",MAT_C_DOUBLE,MAT_T_DOUBLE,2,
				dims2,(void*)b,0);
      matvar[2] = Mat_VarCreate("scal",MAT_C_DOUBLE,MAT_T_DOUBLE,2,
				dims3,(void*)c,0);
      matvar[3] = NULL;
      struct_matvar = Mat_VarCreate("struct_3f",MAT_C_STRUCT,
                              MAT_T_STRUCT,2,dims_struct,matvar,0);
      
      Mat_VarWrite( mat, struct_matvar, 0);

      free(matvar[0]);
      free(matvar[1]);
      free(matvar[2]);
      free(matvar);
      Mat_VarFree(struct_matvar);
      Mat_Close(mat);
    }
  else
    return 1;
    
  return 0;
}

More examples in C

Can be found in the MatIO distribution, particularly the file test_mat.c.

Examples in Fortran

Fortran 90 code

PROGRAM TEST
    USE MATIO
    IMPLICIT NONE
    REAL(8)         :: VEC(5) = (/43.5,5432.434,3.32,0.0001,88834.0/)
    REAL(8)         :: MAT1(2,2) = &
                       RESHAPE((/43.43,565.32,4.2,1.542/), SHAPE(MAT1))
    INTEGER         :: IERR
    TYPE(MAT_T)     :: MAT
    TYPE(MATVAR_T)  :: MATVAR

    ! INITIALIZE FMAT SYSTEM (USE PROGRAM NAME AS SYMBOL)
    CALL FMAT_LOGINIT('test')

    ! OPEN FILE FOR WRITING
    IERR = FMAT_CREATE('test_out.mat', MAT)

    ! WRITE FIRST VARIABLE
    IERR = FMAT_VARCREATE('vec', VEC, MATVAR)    
    IERR = FMAT_VARWRITE(MAT, MATVAR, VEC)
    IERR = FMAT_VARFREE(MATVAR)

    ! WRITE SECOND VARIABLE
    IERR = FMAT_VARCREATE('mat', MAT1, MATVAR)
    IERR = FMAT_VARWRITE(MAT, MATVAR, MAT1)
    IERR = FMAT_VARFREE(MATVAR)

    ! FINISH UP                                                                 
    IERR = FMAT_CLOSE(MAT)
END PROGRAM TEST

To compile; first add the matio module and the intel fortran compiler. Then run

ifort -o test test.f90 -lmatio -lz
Personal tools