core/matlab/examples/eng_mat/matdgns.c

00001 /*
00002  * MAT-file diagnose program
00003  *
00004  * See the MATLAB API Guide for compiling information.
00005  *
00006  * Calling syntax:
00007  *
00008  *   matdgns <matfile>
00009  *
00010  * It will diagnose the MAT-file named <matfile>.
00011  *
00012  * This program demonstrates the use of the following functions:
00013  *
00014  *  matClose
00015  *  matGetDir
00016  *  matGetNextVariable
00017  *  matGetNextVariableInfo
00018  *  matOpen
00019  *
00020  * Copyright 1984-2003 The MathWorks, Inc.
00021  */
00022 /* $Revision: 1.1 $ */
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include "mat.h"
00026 
00027 int diagnose(const char *file) {
00028   MATFile *pmat;
00029   const char **dir;
00030   const char *name;
00031   int     ndir;
00032   int     i;
00033   mxArray *pa;
00034   
00035   printf("Reading file %s...\n\n", file);
00036 
00037   /*
00038    * Open file to get directory
00039    */
00040   pmat = matOpen(file, "r");
00041   if (pmat == NULL) {
00042     printf("Error opening file %s\n", file);
00043     return(1);
00044   }
00045   
00046   /*
00047    * get directory of MAT-file
00048    */
00049   dir = (const char **)matGetDir(pmat, &ndir);
00050   if (dir == NULL) {
00051     printf("Error reading directory of file %s\n", file);
00052     return(1);
00053   } else {
00054     printf("Directory of %s:\n", file);
00055     for (i=0; i < ndir; i++)
00056       printf("%s\n",dir[i]);
00057   }
00058   mxFree(dir);
00059 
00060   /* In order to use matGetNextXXX correctly, reopen file to read in headers. */
00061   if (matClose(pmat) != 0) {
00062     printf("Error closing file %s\n",file);
00063     return(1);
00064   }
00065   pmat = matOpen(file, "r");
00066   if (pmat == NULL) {
00067     printf("Error reopening file %s\n", file);
00068     return(1);
00069   }
00070 
00071   /* Get headers of all variables */
00072   printf("\nExamining the header for each variable:\n");
00073   for (i=0; i < ndir; i++) {
00074     pa = matGetNextVariableInfo(pmat, &name);
00075     if (pa == NULL) {
00076     printf("Error reading in file %s\n", file);
00077     return(1);
00078     }
00079     /* Diagnose header pa */
00080     printf("According to its header, array %s has %d dimensions\n",
00081        name, mxGetNumberOfDimensions(pa));
00082     if (mxIsFromGlobalWS(pa))
00083       printf("  and was a global variable when saved\n");
00084     else
00085       printf("  and was a local variable when saved\n");
00086     mxDestroyArray(pa);
00087   }
00088 
00089   /* Reopen file to read in actual arrays. */
00090   if (matClose(pmat) != 0) {
00091     printf("Error closing file %s\n",file);
00092     return(1);
00093   }
00094   pmat = matOpen(file, "r");
00095   if (pmat == NULL) {
00096     printf("Error reopening file %s\n", file);
00097     return(1);
00098   }
00099 
00100   /* Read in each array. */
00101   printf("\nReading in the actual array contents:\n");
00102   for (i=0; i<ndir; i++) {
00103       pa = matGetNextVariable(pmat, &name);
00104       if (pa == NULL) {
00105       printf("Error reading in file %s\n", file);
00106       return(1);
00107       } 
00108       /*
00109        * Diagnose array pa
00110        */
00111       printf("According to its contents, array %s has %d dimensions\n",
00112          name, mxGetNumberOfDimensions(pa));
00113       if (mxIsFromGlobalWS(pa))
00114     printf("  and was a global variable when saved\n");
00115       else
00116     printf("  and was a local variable when saved\n");
00117       mxDestroyArray(pa);
00118   }
00119 
00120   if (matClose(pmat) != 0) {
00121       printf("Error closing file %s\n",file);
00122       return(1);
00123   }
00124   printf("Done\n");
00125   return(0);
00126 }
00127 
00128 int main(int argc, char **argv)
00129 {
00130   
00131   int result;
00132 
00133   if (argc > 1)
00134     result = diagnose(argv[1]);
00135   else{
00136     result = 0;
00137     printf("Usage: matdgns <matfile>");
00138     printf(" where <matfile> is the name of the MAT-file");
00139     printf(" to be diagnosed\n");
00140   }
00141 
00142   return (result==0)?EXIT_SUCCESS:EXIT_FAILURE;
00143 
00144 }

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy