MAT-File Format - SERC
MAT-File Format - SERC MAT-File Format - SERC
1 MAT-File FormatTable 1-8: Level 4 MAT-File Matrix Header Format (Continued)mrowsncolsimagfnamlenThe row dimension contains an integer with the number of rows in the matrix.The column dimension contains an integer with the number of columns in the matrix.The imaginary flag is an integer whose value is either 0 or 1. If 1, then the matrix hasan imaginary part. If 0, there is only real data.The name length contains an integer with 1 plus the length of the matrix name.Immediately following the fixed length header is the data whose length isdependent on the variables in the fixed length header:Table 1-9: Level 4 MAT-File Matrix Data FormatFieldnamerealimagDescriptionThe matrix name consists of namlen ASCII bytes, the last oneof which must be a null character ('\0').Real part of the matrix consists of mrows ∗ ncols numbers inthe format specified by the P element of the type flag. Thedata is stored column-wise such that the second columnfollows the first column, etc.Imaginary part of the matrix, if any. If the imaginary flagimagf is nonzero, the imaginary part of a matrix is placedhere. It is stored in the same manner as the real data.This structure is repeated for each matrix stored in the file.The following C language code demonstrates how to write a single matrix todisk in Level 1.0 MAT-file format.#include main() {typedef struct {long type;long mrows;long ncols;long imagf;1-38
Level 4 MAT-File Formatlong namelen;} Fmatrix;char *pname;double *pr;double *pi;Fmatrix x;int mn;FILE *fp;double real_data = 1.0;double imag_data = 2.0;fp = fopen("mymatfile.mat", "wb");if (fp != NULL) {pname = "x";x.type = 1000;x.mrows = 1;x.ncols = 1;x.imagf = 1;x.namelen = 2;pr = &real_data;pi = &imag_data;fwrite(&x, sizeof(Fmatrix), 1, fp);fwrite(pname, sizeof(char), x.namelen, fp);mn = x.mrows *x.ncols;fwrite(pr, sizeof(double), mn, fp);if(x.imagf)fwrite(pi, sizeof(double), mn, fp);}elseprintf("File could not be opened.\n");fclose(fp);}1-39
- Page 1 and 2: MATLAB®The Language of Technical C
- Page 3: Revision History:June 1999 Online o
- Page 6 and 7: 1 MAT-File FormatIntroductionThis d
- Page 8 and 9: 1 MAT-File FormatLevel 5 MAT-File F
- Page 10 and 11: 1 MAT-File FormatMAT-File Header Fo
- Page 12 and 13: 1 MAT-File FormatData Element Forma
- Page 14 and 15: 1 MAT-File FormatFor character data
- Page 17 and 18: Level 5 MAT-File FormatEach variabl
- Page 19 and 20: Level 5 MATLAB Array Data Element F
- Page 21 and 22: Level 5 MATLAB Array Data Element F
- Page 23 and 24: Level 5 MATLAB Array Data Element F
- Page 25 and 26: Level 5 MATLAB Array Data Element F
- Page 27 and 28: Level 5 MATLAB Array Data Element F
- Page 29: Level 5 MATLAB Array Data Element F
- Page 32 and 33: 1 MAT-File Formatsee “Numeric Arr
- Page 34 and 35: 1 MAT-File FormatDimensions Array S
- Page 36 and 37: 1 MAT-File FormatFigure 1-14: Examp
- Page 38 and 39: 1 MAT-File FormatClass. This field
- Page 40 and 41: 1 MAT-File FormatLevel 4 MAT-File F
- Page 44 and 45: 1 MAT-File FormatAgain, we strongly
- Page 46 and 47: IndexHheaderdefined 1-4flag fields
- Page 48: IndexIndex-4
Level 4 <strong>MAT</strong>-<strong>File</strong> <strong>Format</strong>long namelen;} Fmatrix;char *pname;double *pr;double *pi;Fmatrix x;int mn;FILE *fp;double real_data = 1.0;double imag_data = 2.0;fp = fopen("mymatfile.mat", "wb");if (fp != NULL) {pname = "x";x.type = 1000;x.mrows = 1;x.ncols = 1;x.imagf = 1;x.namelen = 2;pr = &real_data;pi = &imag_data;fwrite(&x, sizeof(Fmatrix), 1, fp);fwrite(pname, sizeof(char), x.namelen, fp);mn = x.mrows *x.ncols;fwrite(pr, sizeof(double), mn, fp);if(x.imagf)fwrite(pi, sizeof(double), mn, fp);}elseprintf("<strong>File</strong> could not be opened.\n");fclose(fp);}1-39