Minimal interface to matIO

From NA-Wiki

Jump to: navigation, search

MatIO is a powerful library for writing data from from a C/C++/FORTRAN program to the Matlab .mat format (cf. MatIO). This page demonstrates a simple interface to MatIO that simplifies the most common task: Writing a double precison matrix, vector or scalar to the mat-file.

Download the source code here.

mat_out contains three functions:

  • matOutInit("filename.mat") opens the file filename.mat for writing
  • matOutClose() closes the file (there can only be one file open)
  • matOutDouble(array, dimensions, name) writes the data in array onto the mat-file.
    • Arguments:
      • array - pointer to double - the data to be written
      • dims - integer array of length 2 - the dimensions of the array
      • name - a string - the name that the array will have in the matlab workspace
    • Consecutive calls to matOutDouble adds that variable to the mat-file workspace-to-become.

Example 1

#include "matOut.h"

  int main(void)
  {
    double vec[5] = {43.5, 5432.434, 3.32, 0.0001, 88834.0};
    int dims_vec[2] = {1,5};
    double mat1[2][2] = {43.43, 565.32, 4.2, 1.542};
    int dims_mat[2] = {2,2};
  
    matOutInit("test_out.mat");
    matOutDouble(vec,dims_vec,"vec");
    matOutDouble(mat1,dims_mat,"mat");
    matOutClose();
 
  return 0;
  }

Example 2 - A wave equation solver that uses mat_out

The tarball contains a folder example that contains the wave solver demo. Build with the makefile and run the wave_FDM program. Output is stored in wave_output.mat. Load it in matlab and plot

>> load wave_output
>> whos
  Name        Size              Bytes  Class     Attributes

  t         201x1                1608  double              
  u         201x101            162408  double              
  x           1x101               808  double              

>> mesh(x,t,u)
Personal tools