声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2529|回复: 0

[编程技巧] [转帖]*.mat文件的内容,如何分析?

[复制链接]
发表于 2005-7-26 21:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
发信人: FangQ (光芒之神~等Offer中), 信区: MathTools <BR>标  题: Re: *.mat文件的内容,如何分析? <BR>发信站: BBS 水木清华站 (Wed Apr 19 22:25:01 2000) <BR>  <BR>好想是matlab的C库里面的吧,下面两个函数完成mat文件的d的读入和保存 <BR>  <BR>/* <BR> * loadmat - C language routine to load a matrix from a MAT-file. <BR> * <BR> * Here is an example that uses 'loadmat' to load a matrix from a MAT-file: <BR> * <BR> *      FILE *fp; <BR> *      char name[20]; <BR> *      int type, mrows, ncols, imagf; <BR> *      double *xr, *xi; <BR> *      fp = fopen("foo.mat","rb"); <BR> *      loadmat(fp, &amp;type, name, &amp;mrows, &amp;ncols, &amp;imagf, &amp;xr, &amp;xi); <BR> *      fclose(fp); <BR> *      free(xr); <BR> *      if (imagf) free(xi); <BR> * <BR> * The 'loadmat' routine returns 0 if the read is successful and 1 if <BR> * and end-of-file or read error is encountered.  'loadmat' can be called <BR> * repeatedly until the file is exhausted and an EOF is encountered. <BR> * <BR> * Author J.N. Little 11-3-86 <BR> * <BR> * Modified by Steffen Torp, Servolaboratoriet 14-10-92. <BR> */ <BR>int loadmat(FILE *fp,int *type,char *pname,int *mrows,int *ncols,int *imagf, <BR>double **preal,double **pimag) <BR>/* FILE *fp;        File pointer */ <BR>/* int *type;       Type flag: see reference section of guide */ <BR>/* int *mrows;      row dimension */ <BR>/* int *ncols;      column dimension */ <BR>/* int *imagf;      imaginary flag */ <BR>/* char *pname;     pointer to matrix name */ <BR>/* double **preal;  pointer to real data */ <BR>/* double **pimag;  pointer to imag data */ <BR>{ <BR> Fmatrix x; <BR> int mn, namlen, conv; <BR> /* <BR>  * Get Fmatrix structure from file <BR>  */ <BR> if (fread((char *)&amp;x, sizeof(Fmatrix), 1, fp) != 1) { <BR>  return(1); <BR> } <BR> /* Check if the mat-file is created on a platform similar to */ <BR> /* the actual platform. If yes, the flag 'conv' will be FALSE*/ <BR> /* Otherwise it will be TRUE                                 */ <BR> if (SYSTEM == 0) { <BR>  switch (x.type) { <BR>   case 0L:          conv = FALSE; break; <BR>   case 1L:          conv = FALSE; break; <BR>   case 0xE8030000L: conv = TRUE;  break; <BR>   case 0xE9030000L: conv = TRUE;  break; <BR>   default: { <BR>    printf("\nUnknown file type in loadmat!\n"); <BR>    printf("The type is: %lx \n",x.type); <BR>    exit(1); <BR>   } <BR>  } <BR> } <BR> else if (SYSTEM == 1000) { <BR>  switch (x.type) { <BR>   case 0L:         conv = TRUE;  break; <BR>   case 0x1000000L: conv = TRUE;  break; <BR>   case 1000L:      conv = FALSE; break; <BR>   case 1001L:      conv = FALSE; break; <BR>   default: { <BR>    printf("\nUnknown file type in loadmat!\n"); <BR>    printf("The type is: %lx \n",x.type); <BR>    exit(1); <BR>   } <BR>  } <BR> } <BR> if(conv==TRUE) <BR> { <BR>  *type  = (int)longconv(x.type); <BR>  *mrows = (int)longconv(x.mrows); <BR>  *ncols = (int)longconv(x.ncols); <BR>  *imagf = (int)longconv(x.imagf); <BR>  namlen = (int)longconv(x.namlen); <BR>  mn     = (*mrows)*(*ncols); <BR> } <BR> else <BR> { <BR>  *type  = (int)(x.type); <BR>  *mrows = (int)(x.mrows); <BR>  *ncols = (int)(x.ncols); <BR>  *imagf = (int)(x.imagf); <BR>  namlen = (int)(x.namlen); <BR>  mn     = (*mrows)*(*ncols); <BR> } <BR> /* <BR>  * Get matrix name from file <BR>  */ <BR> if (fread(pname, sizeof(char), namlen, fp) != namlen) { <BR>  return(1); <BR> } <BR> /* <BR>  * Get Real part of matrix from file <BR>  */ <BR> if (mn) { <BR>  *preal = (double *)malloc(mn*sizeof(double)); <BR>  if (!(*preal)) { <BR>   printf("\nError: Variable too big to load\n"); <BR>   return(1); <BR>  } <BR>  if (fread(*preal, sizeof(double), mn, fp) != mn) { <BR>   free(*preal); <BR>   return(1); <BR>  } <BR>  if(conv==TRUE) matconv(mn,*preal); <BR> } <BR> /* <BR>  * Get Imag part of matrix from file, if it exists <BR>  */ <BR> if (x.imagf) { <BR>  *pimag = (double *)malloc(mn*sizeof(double)); <BR>  if (!(*pimag)) { <BR>   printf("\nError: Variable too big to load\n"); <BR>   free(*preal); <BR>   return(1); <BR>  } <BR>  if (fread(*pimag, sizeof(double), mn, fp) != mn) { <BR>   free(*pimag); <BR>   free(*preal); <BR>   return(1); <BR>  } <BR>  if(conv==TRUE) matconv(mn,*pimag); <BR> } <BR> return(0); <BR>} <BR>/* <BR> * savemat - C language routine to save a matrix in a MAT-file. <BR> * <BR> * Here is an example that uses 'savemat' to save two matrices to disk, <BR> * the second of which is complex: <BR> * <BR> *      FILE *fp; <BR> *      double xyz[1000], ar[1000], ai[1000]; <BR> *      fp = fopen("foo.mat","wb"); <BR> *      savemat(fp, 2000, "xyz", 2, 3, 0, xyz, (double *)0); <BR> *      savemat(fp, 2000, "a", 5, 5, 1, ar, ai); <BR> *      fclose(fp); <BR> * <BR> * Author J.N. Little 11-3-86 <BR> */ <BR>void savemat(FILE *fp,int type,char *pname,int mrows,int ncols,int imagf,dou <BR>ble *preal,double *pimag) <BR>/* FILE *fp;      File pointer */ <BR>/* int type;      Type flag: Normally 0 for PC, 1000 for Sun, Mac, and  */ <BR>/*                Apollo, 2000 for VAX D-float, 3000 for VAX G-float    */ <BR>/*                Add 1 for text variables.      */ <BR>/*                See LOAD in reference section of guide for more info. */ <BR>/* int mrows;     row dimension */ <BR>/* int ncols;     column dimension */ <BR>/* int imagf;     imaginary flag */ <BR>/* char *pname;   pointer to matrix name */ <BR>/* double *preal; pointer to real data */ <BR>/* double *pimag; pointer to imag data */ <BR>{ <BR> Fmatrix x; <BR> int mn; <BR> x.type   = (long)type; <BR> x.mrows  = (long)mrows; <BR> x.ncols  = (long)ncols; <BR> x.imagf  = (long)imagf; <BR> x.namlen = (long)strlen(pname) + 1L; <BR> mn       = (int)(x.mrows * x.ncols); <BR> fwrite(&amp;x, sizeof(Fmatrix), 1, fp); <BR> fwrite(pname, sizeof(char), (int)x.namlen, fp); <BR> fwrite(preal, sizeof(double), mn, fp); <BR> if (imagf) { <BR><BR>      fwrite(pimag, sizeof(double), mn, fp); <BR> } <BR>} <BR>
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-6 03:38 , Processed in 0.049749 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表