| MATLAB Application Program Interface | Search  Help Desk |
| matGetDir | Examples |
Get directory of mxArrays in a MAT-file
#include "mat.h" char **matGetDir(MATFile *mfp, int *num);
mfp
num
matGetDir returns a pointer to an internal array containing pointers to the NULL-terminated names of the mxArrays in the MAT-file pointed to by mfp. The length of the internal array (number of mxArrays 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 and sets num to a negative number if it fails. If num is zero, mfp contains no arrays.
MATLAB variable names can be up to length mxMAXNAM, where mxMAXNAM is defined in the file matrix.h.
Print out a directory of the mxArray names contained within a MAT-file:
/* mattest7.c */
#include <stdio.h>
#include "mat.h"
void main() {
MATFile *mfp;
char **dir;
int ndir,i;
mfp = matOpen("foo.mat","r");
dir = matGetDir(mfp,&ndir);
matClose(mfp);
if (dir == NULL && ndir < 0) {
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);
}
See matcreat.c and matdgns.c in the eng_mat subdirectory of the examples directory for sample programs that illustrate how to use the MATLAB MAT-file routines in a C program.