NEURON
zmatlab.c
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /**************************************************************************
4 **
5 ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
6 **
7 ** Meschach Library
8 **
9 ** This Meschach Library is provided "as is" without any express
10 ** or implied warranty of any kind with respect to this software.
11 ** In particular the authors shall not be liable for any direct,
12 ** indirect, special, incidental or consequential damages arising
13 ** in any way from use of the software.
14 **
15 ** Everyone is granted permission to copy, modify and redistribute this
16 ** Meschach Library, provided:
17 ** 1. All copies contain this copyright notice.
18 ** 2. All modified copies shall carry a notice stating who
19 ** made the last modification and the date of such modification.
20 ** 3. No charge is made for this software or works derived from it.
21 ** This clause shall not be construed as constraining other software
22 ** distributed on the same medium as this software, nor is a
23 ** distribution fee considered a charge.
24 **
25 ***************************************************************************/
26 
27 
28 /*
29  This file contains routines for import/exporting complex data
30  to/from MATLAB. The main routines are:
31  ZMAT *zm_save(FILE *fp,ZMAT *A,char *name)
32  ZVEC *zv_save(FILE *fp,ZVEC *x,char *name)
33  complex z_save(FILE *fp,complex z,char *name)
34  ZMAT *zm_load(FILE *fp,char **name)
35 */
36 
37 #include <stdio.h>
38 #include "zmatrix.h"
39 #include "matlab.h"
40 
41 static char rcsid[] = "zmatlab.c,v 1.1 1997/12/04 17:56:11 hines Exp";
42 
43 /* zm_save -- save matrix in ".mat" file for MATLAB
44  -- returns matrix to be saved */
46 FILE *fp;
47 ZMAT *A;
48 char *name;
49 {
50  int i, j;
51  matlab mat;
52 
53  if ( ! A )
54  error(E_NULL,"zm_save");
55 
56  mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
57  mat.m = A->m;
58  mat.n = A->n;
59  mat.imag = TRUE;
60  mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
61 
62  /* write header */
63  fwrite(&mat,sizeof(matlab),1,fp);
64  /* write name */
65  if ( name == (char *)NULL )
66  fwrite("",sizeof(char),1,fp);
67  else
68  fwrite(name,sizeof(char),(int)(mat.namlen),fp);
69  /* write actual data */
70 #if ORDER == ROW_ORDER
71  for ( i = 0; i < A->m; i++ )
72  for ( j = 0; j < A->n; j++ )
73  fwrite(&(A->me[i][j].re),sizeof(Real),1,fp);
74  for ( i = 0; i < A->m; i++ )
75  for ( j = 0; j < A->n; j++ )
76  fwrite(&(A->me[i][j].im),sizeof(Real),1,fp);
77 #else /* column major order: ORDER == COL_ORDER */
78  for ( j = 0; j < A->n; j++ )
79  for ( i = 0; i < A->m; i++ )
80  fwrite(&(A->me[i][j].re),sizeof(Real),1,fp);
81  for ( j = 0; j < A->n; j++ )
82  for ( i = 0; i < A->m; i++ )
83  fwrite(&(A->me[i][j].im),sizeof(Real),1,fp);
84 #endif
85 
86  return A;
87 }
88 
89 
90 /* zv_save -- save vector in ".mat" file for MATLAB
91  -- saves it as a row vector
92  -- returns vector to be saved */
94 FILE *fp;
95 ZVEC *x;
96 char *name;
97 {
98  int i;
99  matlab mat;
100 
101  if ( ! x )
102  error(E_NULL,"zv_save");
103 
104  mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
105  mat.m = x->dim;
106  mat.n = 1;
107  mat.imag = TRUE;
108  mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
109 
110  /* write header */
111  fwrite(&mat,sizeof(matlab),1,fp);
112  /* write name */
113  if ( name == (char *)NULL )
114  fwrite("",sizeof(char),1,fp);
115  else
116  fwrite(name,sizeof(char),(int)(mat.namlen),fp);
117  /* write actual data */
118  for ( i = 0; i < x->dim; i++ )
119  fwrite(&(x->ve[i].re),sizeof(Real),1,fp);
120  for ( i = 0; i < x->dim; i++ )
121  fwrite(&(x->ve[i].im),sizeof(Real),1,fp);
122 
123  return x;
124 }
125 
126 /* z_save -- saves complex number in ".mat" file for MATLAB
127  -- returns complex number to be saved */
129 FILE *fp;
130 complex z;
131 char *name;
132 {
133  matlab mat;
134 
135  mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
136  mat.m = 1;
137  mat.n = 1;
138  mat.imag = TRUE;
139  mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
140 
141  /* write header */
142  fwrite(&mat,sizeof(matlab),1,fp);
143  /* write name */
144  if ( name == (char *)NULL )
145  fwrite("",sizeof(char),1,fp);
146  else
147  fwrite(name,sizeof(char),(int)(mat.namlen),fp);
148  /* write actual data */
149  fwrite(&z,sizeof(complex),1,fp);
150 
151  return z;
152 }
153 
154 
155 
156 /* zm_load -- loads in a ".mat" file variable as produced by MATLAB
157  -- matrix returned; imaginary parts ignored */
159 FILE *fp;
160 char **name;
161 {
162  ZMAT *A;
163  int i;
164  int m_flag, o_flag, p_flag, t_flag;
165  float f_temp;
166  double d_temp;
167  matlab mat;
168 
169  if ( fread(&mat,sizeof(matlab),1,fp) != 1 )
170  error(E_FORMAT,"zm_load");
171  if ( mat.type >= 10000 ) /* don't load a sparse matrix! */
172  error(E_FORMAT,"zm_load");
173  m_flag = (mat.type/1000) % 10;
174  o_flag = (mat.type/100) % 10;
175  p_flag = (mat.type/10) % 10;
176  t_flag = (mat.type) % 10;
177  if ( m_flag != MACH_ID )
178  error(E_FORMAT,"zm_load");
179  if ( t_flag != 0 )
180  error(E_FORMAT,"zm_load");
181  if ( p_flag != DOUBLE_PREC && p_flag != SINGLE_PREC )
182  error(E_FORMAT,"zm_load");
183  *name = (char *)malloc((unsigned)(mat.namlen)+1);
184  if ( fread(*name,sizeof(char),(unsigned)(mat.namlen),fp) == 0 )
185  error(E_FORMAT,"zm_load");
186  A = zm_get((unsigned)(mat.m),(unsigned)(mat.n));
187  for ( i = 0; i < A->m*A->n; i++ )
188  {
189  if ( p_flag == DOUBLE_PREC ) {
190  if (fread(&d_temp,sizeof(double),1,fp) != 1) {error(E_INPUT, "zm_load");}
191  } else {
192  if (fread(&f_temp,sizeof(float),1,fp) != 1) {error(E_INPUT, "zm_load");}
193  d_temp = f_temp;
194  }
195  if ( o_flag == ROW_ORDER ) {
196  A->me[i / A->n][i % A->n].re = d_temp;
197  } else if ( o_flag == COL_ORDER ) {
198  A->me[i % A->m][i / A->m].re = d_temp;
199  } else {
200  error(E_FORMAT,"zm_load");
201  }
202  }
203 
204  if ( mat.imag ) /* skip imaginary part */
205  for ( i = 0; i < A->m*A->n; i++ )
206  {
207  if ( p_flag == DOUBLE_PREC ) {
208  if (fread(&d_temp,sizeof(double),1,fp) != 1) {error(E_INPUT, "zm_load");}
209  } else {
210  if (fread(&f_temp,sizeof(float),1,fp) != 1) {error(E_INPUT, "zm_load");}
211  d_temp = f_temp;
212  }
213  if ( o_flag == ROW_ORDER )
214  A->me[i / A->n][i % A->n].im = d_temp;
215  else if ( o_flag == COL_ORDER )
216  A->me[i % A->m][i / A->m].im = d_temp;
217  else
218  error(E_FORMAT,"zm_load");
219  }
220 
221  return A;
222 }
223 
#define SINGLE_PREC
Definition: matlab.h:58
#define PRECISION
Definition: matlab.h:71
Real im
Definition: zmatrix.h:40
u_int dim
Definition: zmatrix.h:45
complex z_save(FILE *fp, complex z, char *name)
Definition: zmatlab.c:128
#define E_INPUT
Definition: err.h:101
long imag
Definition: matlab.h:39
#define Real
Definition: machine.h:189
u_int m
Definition: zmatrix.h:51
#define TRUE
Definition: err.c:57
Definition: zmatrix.h:50
#define ORDER
Definition: matlab.h:68
Definition: zmatrix.h:44
static Frame * fp
Definition: code.cpp:154
#define E_FORMAT
Definition: err.h:100
#define COL_ORDER
Definition: matlab.h:54
complex * ve
Definition: zmatrix.h:46
static char rcsid[]
Definition: zmatlab.c:41
Real re
Definition: zmatrix.h:40
#define DOUBLE_PREC
Definition: matlab.h:57
#define E_NULL
Definition: err.h:102
size_t j
char * name
Definition: init.cpp:16
long type
Definition: matlab.h:36
#define ROW_ORDER
Definition: matlab.h:55
ZMAT * zm_save(FILE *fp, ZMAT *A, char *name)
Definition: zmatlab.c:45
ZMAT * zm_get(int m, int n)
Definition: zmemory.c:65
u_int n
Definition: zmatrix.h:51
#define i
Definition: md1redef.h:12
#define A(i)
Definition: multisplit.cpp:61
ZVEC * zv_save(FILE *fp, ZVEC *x, char *name)
Definition: zmatlab.c:93
#define MACH_ID
Definition: matlab.h:65
#define error(err_num, fn_name)
Definition: err.h:73
ZMAT * zm_load(FILE *fp, char **name)
Definition: zmatlab.c:158
complex ** me
Definition: zmatrix.h:54
long m
Definition: matlab.h:37
Definition: matlab.h:35
return NULL
Definition: cabcode.cpp:461
long namlen
Definition: matlab.h:40
long n
Definition: matlab.h:38