⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whomat.c

📁 《精通matlab7》“mastering matlab 7”的代码。
💻 C
字号:
/* *  whomat.c - Examine a binary MAT file and print a list *             of the contents (like "who" or "whos"). * *  Mastering MATLAB 7 C MAT-file Example 2 * */#include "mat.h"#include <string.h>int whomat(const char *filename) {  MATFile *mfile;  mxArray *marray;  char **dir;  char siz[25], buf[10];  int i, j, num, nel, elsize, ndim, eltot, btot;  const int *dims;    /* Open the MAT file for reading. */  mfile = matOpen(filename, "r");  if (mfile == NULL) {    printf("Cannot open %s for reading.\n", filename);    return(EXIT_FAILURE);  }   /* Get the directory list and print in "who" format. */  dir = matGetDir(mfile, &num);  if (dir == NULL) {    printf("Error reading the directory of %s.\n", filename);    return(EXIT_FAILURE);  } else {    printf("\n");    printf("Variables in %s are:\n\n", filename);    for (i=0; i<num; i++) {      printf("%-10s",dir[i]);      if (i>0 && i%4==0) printf("\n");    }  }  /* Examine each variable and print a "whos" list. */  eltot=btot=0;  printf("\n\n  Name         Size         Bytes  Class\n\n");  for (i=0; i<num; i++) {    marray=matGetVariable(mfile, dir[i]);    if (marray == NULL) {      printf("Cannot read file %s.\n\n", filename);      return(EXIT_FAILURE);     }    /* If marray is a cell array or structure array, then  */    /* mxGetElementSize returns the size of a pointer, not */    /* the size of all the elements in each cell or field. */    /* To get the correct number of bytes would require    */    /* traversing the array and summing leaf element sizes.*/    /* Java arrays return 0x0 array dimensions and 0 size. */    elsize=mxGetElementSize(marray);    btot=btot+(nel*elsize);    nel=mxGetNumberOfElements(marray);    eltot=eltot+nel;    ndim=mxGetNumberOfDimensions(marray);    dims=mxGetDimensions(marray);    siz[0]='\0';    for (j=0; j<ndim; j++) {      sprintf(buf,"%d",dims[j]);      strcat(siz,buf);      if (j<(ndim-1))        strcat(siz,"x");    }    printf("  %-12s %-12s %5d  %s array\n", dir[i],              siz,nel*elsize,mxGetClassName(marray));    mxDestroyArray(marray);  }  printf("\nGrand total is %d elements using %d bytes\n\n",            eltot,btot);  /* Release the memory allocated for the directory. */   mxFree(dir);  /* Close the MAT file. */  if (matClose(mfile) != 0) {      printf("Cannot close %s.\n",filename);      return(EXIT_FAILURE);  }  return(EXIT_SUCCESS);}int main(int argc, char **argv){  int status;  if (argc > 1)    status = whomat(argv[1]);  else{    status = EXIT_FAILURE;    printf("Usage: whomat <matfile>");  }  return(status);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -