home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / SRC.DI$ / MEXMAT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-26  |  2.0 KB  |  107 lines

  1. /*
  2.  * mexmat.c MEX MAT-File Test Program
  3.  * 
  4.  * To compile, type:
  5.  *
  6.  *     cmex mexmat.c
  7.  *
  8.  */
  9.  
  10. #include "mat.h"   /* Must include mat.h before mex.h */
  11. #include "mex.h"   /* because mat.h #includes stdio.h */
  12.  
  13. #ifdef __STDC__
  14. void mexFunction(int nlhs, Matrix *plhs[], int nrhs, Matrix *prhs[])
  15. #else
  16. mexFunction(nlhs, plhs, nrhs, prhs)
  17.     int nlhs;
  18.     Matrix *plhs[];
  19.     int nrhs;
  20.     Matrix *prhs[];
  21. #endif
  22. {
  23.     MATFile    *ph;
  24.     MATFile    *ph1;
  25.     char        **dir;
  26.     int            ndir;
  27.     int            i;
  28.     Matrix        *pmat;
  29.  
  30.  
  31.     /*
  32.      * open "matlab.mat"
  33.      */
  34.     ph = matOpen("matlab.mat", "u");
  35.  
  36.     if (ph == NULL) {
  37.         mexErrMsgTxt("No matlab.mat file.\n");
  38.     }
  39.  
  40.     /*
  41.      * copy matlab.mat to temp.mat by using matGetNextMatrix
  42.      */
  43.     ph1 = matOpen("temp.mat", "w");
  44.  
  45.     if (ph1 == NULL) {
  46.         mexErrMsgTxt("Not able to make temp.mat.\n");
  47.     }
  48.  
  49.     for (;;) {
  50.         pmat = matGetNextMatrix(ph);
  51.         if (pmat == NULL) {
  52.             if (ferror(matGetFp(ph))) {
  53.                 mexErrMsgTxt("Read error using matGetNextMatrix on matlab.mat.\n");
  54.             } else {
  55.                 break;
  56.             }
  57.         }
  58.         if (matPutMatrix(ph1,pmat)) {
  59.             mexErrMsgTxt("Error writing matrix to temp.mat.\n");
  60.         }
  61.         mxFreeMatrix(pmat);
  62.     }
  63.  
  64.     matClose(ph1);
  65.  
  66.     /*
  67.      * get directory of matlab.mat
  68.      */
  69.     dir = matGetDir(ph, &ndir);
  70.  
  71.     if (dir == NULL) {
  72.         mexErrMsgTxt("Directory reading problem.\n");
  73.     } else {
  74.         printf("Directory of matlab.mat\n");
  75.         for (i=0; i < ndir; i++)
  76.             printf("%s\n",dir[i]);
  77.     }
  78.  
  79.     ph1 = matOpen("temp1.mat", "w");
  80.  
  81.     if (ph1 == NULL) {
  82.         mexErrMsgTxt("Not able to make temp1.mat.\n");
  83.     }
  84.  
  85.     /*
  86.      * copy matlab.mat to temp1.mat using matGetMatrix
  87.      */
  88.     for (i=0; i < ndir; i++) {
  89.         pmat = matGetMatrix(ph, dir[i]);
  90.         if (pmat == NULL) {
  91.             if (feof(matGetFp(ph))) {
  92.                 mexErrMsgTxt("Read error using matGetMatrix on matlab.mat.\n");
  93.             } else {
  94.                 printf("Could not find matrix %s even though it is in the \
  95. directory.\n",dir[i]);
  96.                 mexErrMsgTxt("");
  97.             }
  98.         }
  99.         if (matPutMatrix(ph1,pmat)) {
  100.             mexErrMsgTxt("Error writing matrix to temp1.mat.\n");
  101.         }
  102.         mxFreeMatrix(pmat);
  103.     }
  104.  
  105.     matClose(ph1);
  106. }
  107.