matGetNextMatrix

Purpose

Get next Matrix from MAT-file.

C Synopsis

#include "mat.h"
Matrix *matGetNextMatrix(fp)
  MATFile *fp;

Fortran Synopsis

integer*4 function matGetNextMatrix(fp)
integer*4 fp

Arguments

fp
MAT-file handle

Description

This routine allows you to step sequentially through a MAT-file and read all the Matrices in a single pass.

matGetNextMatrix reads the next Matrix from the MAT-file pointed to by fp and returns a pointer to a newly allocated Matrix structure. Use it immediately after opening the MAT-file with matOpen and not in conjunction with other MAT-file routines; otherwise, the concept of the next Matrix is undefined.

matGetNextMatrix returns NULL when the end-of-file is reached or if there is an error condition. Use feof and ferror from the Standard C Library to determine status.

Be careful in your code to free the Matrix created by this routine when you are finished with it.

Examples

Print out the row dimensions of all the Matrices in a MAT-file.

/* mattest3.c */ 
#include <string.h>
#include "mat.h"
main()
{
  MATFile *fp;
  Matrix *mp;
  fp = matOpen("foo.mat","r");
  while ((mp = matGetNextMatrix(fp)) != NULL) {
    printf("Row dimension is %d\n",mxGetM(mp));
    mxFreeMatrix(mp);
  }
  matClose(fp);
}
The Fortran version of this code is in mattest3.f:

  program main
  integer matOpen,matClose,mxGetM
  integer mp, fp, stat, matGetNextMatrix
c
  mh = matOpen('foo.mat','r')
  do 10 i = 1,1000
    mp = matGetNextMatrix(fp)
    if (mp .eq. 0) then
      go to 20
    end if
    write(6,*) 'Row dimension is ',mxGetM(mp)
mxFreeMatrix(mp)
10  continue
20  continue
  stat = matClose(fp)
c
  stop
  end

(c) Copyright 1994 by The MathWorks, Inc.