# matGetDir

## Purpose

Get directory of Matrices in a MAT-file.

## C Synopsis

    #include "mat.h"
    char **matGetDir(fp,num)
      MATFile *fp;
      int *num;

## Fortran Synopsis

    integer*4 function matGetDir(fp,num)
    integer*4 fp, num

## Arguments

`fp`
  MAT-file handle
`num`
  number of Matrices in the MAT-file

## Description

This routine allows you to get a list of the names of the Matrices contained within a MAT-file.

`matGetDir` returns a pointer to an internal array containing pointers to the `NULL`-terminated names of the Matrices in the MAT-file pointed to by `fp`. The length of the internal array (number of Matrices in the MAT-file) is placed into `num`. The internal array is allocated using a single `mxCalloc` and must be freed using `mxFree` when you are finished with it.

`matGetDir` returns `NULL` if it fails.

MATLAB variable names can by up to length `mxMAXNAM`, where `mxMAXNAM` is defined in the file `matrix.h`.

## Examples

Print out a directory of the Matrix names contained within a MAT-file:

    /* mattest7.c */
    #include <stdio.h>
    #include "mat.h"
    main() {
      MATFile *fp;
      char **dir;
      int ndir,i;
      fp = matOpen("foo.mat","r");
      dir = matGetDir(fp,&ndir);
      matClose(fp);
      if (dir == NULL) {
        printf("Can''t read directory.\n");
        exit(0);
      } else {
        printf("Directory of MAT-file:\n");
        for (i = 0; i < ndir; i++) {
          printf("%s\n",dir[i]);
        }
      }
      mxFree(dir);
    }

The Fortran version of this program is in `mattest7.f`:

      program main
      integer*4 fp, dir, ndir, adir(100)
      character*20 names(100)
      fp = matOpen('foo.mat','r');
      dir = matGetDir(fp,ndir)
      if (dir .eq. 0) then
        write (6,*) 'Can't read directory.'
        stop
      end if
    c  Copy pointer into an array of pointers
      call mxCopyPtrToInteger4(dir, adir, ndir)
    c  Copy pointer to character string
      do 20 i = 1,ndir
        call mxCopyPtrToCharacter(adir(i),names(i),20)
      20 continue
      write(6,*) 'Directory of MAT-file:'
      do 30 i = 1,ndir
        write(6,*) names(i)
      30 continue
      matClose(fp)
      stop
      end

(c) Copyright 1994 by The MathWorks, Inc.