1 /*
2 Copyright (c) 1990 Massachusetts Institute of Technology, Cambridge, MA.
3 All rights reserved.
4 
5 This Agreement gives you, the LICENSEE, certain rights and obligations.
6 By using the software, you indicate that you have read, understood, and
7 will comply with the terms.
8 
9 Permission to use, copy and modify for internal, noncommercial purposes
10 is hereby granted.  Any distribution of this program or any part thereof
11 is strictly prohibited without prior written consent of M.I.T.
12 
13 Title to copyright to this software and to any associated documentation
14 shall at all times remain with M.I.T. and LICENSEE agrees to preserve
15 same.  LICENSEE agrees not to make any copies except for LICENSEE'S
16 internal noncommercial use, or to use separately any portion of this
17 software without prior written consent of M.I.T.  LICENSEE agrees to
18 place the appropriate copyright notice on any such copies.
19 
20 Nothing in this Agreement shall be construed as conferring rights to use
21 in advertising, publicity or otherwise any trademark or the name of
22 "Massachusetts Institute of Technology" or "M.I.T."
23 
24 M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By
25 way of example, but not limitation, M.I.T. MAKES NO REPRESENTATIONS OR
26 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
27 THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS OR DOCUMENTATION WILL
28 NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
29 M.I.T. shall not be held liable for any liability nor for any direct,
30 indirect or consequential damages with respect to any claim by LICENSEE
31 or any third party on account of or arising from this Agreement or use
32 of this software.
33 */
34 
35 /*
36  * savemat - C language routine to save a matrix in a MAT-file.
37  *
38  * We recommend that you use this routine, and its companion loadmat.c,
39  * for all writing and reading of MAT-files.  These routines implement
40  * "access methods" for MAT-files.  By using these routines, instead
41  * of writing your own code to directly access the MAT-file format,
42  * you will be unaffected by any changes that may be made to the MAT-file
43  * structure at a future date.
44  *
45  * Here is an example that uses 'savemat' to save two matrices to disk,
46  * the second of which is complex:
47  *
48  *	FILE *fp;
49  *	double xyz[1000], ar[1000], ai[1000];
50  *	fp = fopen("foo.mat","wb");
51  *	savemat(fp, 2000, "xyz", 2, 3, 0, xyz, (double *)0);
52  *	savemat(fp, 2000, "a", 5, 5, 1, ar, ai);
53  *      fclose(fp);
54  *
55  * Author J.N. Little 11-3-86
56  * Revised 7-23-91 to support ANSI-C
57  */
58 #include <stdio.h>
59 #include <string.h>
60 
61 typedef struct {
62      long type;   /* type */
63      long mrows;  /* row dimension */
64      long ncols;  /* column dimension */
65      long imagf;  /* flag indicating imag part */
66      long namlen; /* name length (including NULL) */
67 } Fmatrix;
68 
69 #ifdef __STDC__
savemat(FILE * fp,int type,char * pname,int mrows,int ncols,int imagf,double * preal,double * pimag)70 void savemat(FILE *fp, int type, char *pname, int mrows, int ncols,
71              int imagf, double *preal, double *pimag)
72 #else
73 void savemat(fp, type, pname, mrows, ncols, imagf, preal, pimag)
74 FILE *fp;       /* File pointer */
75 int type;       /* Type flag: Normally 0 for PC, 1000 for Sun, Mac, */
76 		/* Apollo, and other Motorola format, */
77 		/* 2000 for VAX D-float, 3000 for VAX G-float, and */
78 		/* 4000 for CRAY */
79 		/* Add 1 for text variables, 2 for sparse matrices */
80 		/* See LOAD in reference section of guide for more info.*/
81 char *pname;    /* pointer to matrix name */
82 int mrows;      /* row dimension */
83 int ncols;      /* column dimension */
84 int imagf;	/* imaginary flag */
85 double *preal;  /* pointer to real data */
86 double *pimag;  /* pointer to imag data */
87 #endif
88 {
89 	Fmatrix x;
90 	int mn;
91 
92 	x.type = type;
93 	x.mrows = mrows;
94 	x.ncols = ncols;
95 	x.imagf = imagf;
96 	x.namlen = strlen(pname) + 1;
97 	mn = x.mrows * x.ncols;
98 
99 	fwrite(&x, sizeof(Fmatrix), 1, fp);
100 	fwrite(pname, sizeof(char), (int)x.namlen, fp);
101 	fwrite(preal, sizeof(double), mn, fp);
102 	if (imagf) {
103 	     fwrite(pimag, sizeof(double), mn, fp);
104 	}
105 }
106 
107 /*
108   MODIFIED version of above: added wr_flag to allow multiple writes
109   to same matrix
110   wr_flag = 0 => open, print header (like old matlab setup)
111   wr_flag = 1 => update, print without header
112 */
113 #ifdef __STDC__
savemat_mod(FILE * fp,int type,char * pname,int mrows,int ncols,int imagf,double * preal,double * pimag,int wr_flag,int mn)114 void savemat_mod(FILE *fp, int type, char *pname, int mrows, int ncols,
115 		 int imagf, double *preal, double *pimag, int wr_flag, int mn)
116 #else
117 void savemat_mod(fp, type, pname, mrows, ncols, imagf, preal, pimag,
118 		 wr_flag, mn)
119 FILE *fp;       /* File pointer */
120 int type;       /* Type flag: Normally 0 for PC, 1000 for Sun, Mac, */
121 		/* Apollo, and other Motorola format, */
122 		/* 2000 for VAX D-float, 3000 for VAX G-float, and */
123 		/* 4000 for CRAY */
124 		/* Add 1 for text variables, 2 for sparse matrices */
125 		/* See LOAD in reference section of guide for more info.*/
126 char *pname;    /* pointer to matrix name */
127 int mrows;      /* row dimension */
128 int ncols;      /* column dimension */
129 int imagf;	/* imaginary flag */
130 double *preal;  /* pointer to real data */
131 double *pimag;  /* pointer to imag data */
132 int wr_flag;			/* 0 for open, 1 to add to matrix */
133 int mn;				/* real #entries, this dump only */
134 #endif
135 {
136 	Fmatrix x;
137 
138 	if(wr_flag == 0) {
139 	  x.type = type;
140 	  x.mrows = mrows;
141 	  x.ncols = ncols;
142 	  x.imagf = imagf;
143 	  x.namlen = strlen(pname) + 1;
144 
145 	  fwrite(&x, sizeof(Fmatrix), 1, fp);
146 	  fwrite(pname, sizeof(char), (int)x.namlen, fp);
147 	}
148 	fwrite(preal, sizeof(double), mn, fp);
149 	if (imagf) {
150 	     fwrite(pimag, sizeof(double), mn, fp);
151 	}
152 }
153