NEURON
matlab.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 data to/from
30  MATLAB. The main routines are:
31  MAT *m_save(FILE *fp,MAT *A,char *name)
32  VEC *v_save(FILE *fp,VEC *x,char *name)
33  MAT *m_load(FILE *fp,char **name)
34 */
35 
36 #include <stdio.h>
37 #include "matrix.h"
38 #include "matlab.h"
39 
40 static char rcsid[] = "matlab.c,v 1.1 1997/12/04 17:55:34 hines Exp";
41 
42 /* m_save -- save matrix in ".mat" file for MATLAB
43  -- returns matrix to be saved */
45 FILE *fp;
46 MAT *A;
47 char *name;
48 {
49  int i;
50  matlab mat;
51 
52  if ( ! A )
53  error(E_NULL,"m_save");
54 
55  mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
56  mat.m = A->m;
57  mat.n = A->n;
58  mat.imag = FALSE;
59  mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
60 
61  /* write header */
62  fwrite(&mat,sizeof(matlab),1,fp);
63  /* write name */
64  if ( name == (char *)NULL )
65  fwrite("",sizeof(char),1,fp);
66  else
67  fwrite(name,sizeof(char),(int)(mat.namlen),fp);
68  /* write actual data */
69 #if ORDER == ROW_ORDER
70  for ( i = 0; i < A->m; i++ )
71  fwrite(A->me[i],sizeof(Real),(int)(A->n),fp);
72 #else /* column major order: ORDER == COL_ORDER */
73  for ( j = 0; j < A->n; j++ )
74  for ( i = 0; i < A->m; i++ )
75  fwrite(&(A->me[i][j]),sizeof(Real),1,fp);
76 #endif
77 
78  return A;
79 }
80 
81 
82 /* v_save -- save vector in ".mat" file for MATLAB
83  -- saves it as a row vector
84  -- returns vector to be saved */
86 FILE *fp;
87 VEC *x;
88 char *name;
89 {
90  matlab mat;
91 
92  if ( ! x )
93  error(E_NULL,"v_save");
94 
95  mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
96  mat.m = x->dim;
97  mat.n = 1;
98  mat.imag = FALSE;
99  mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
100 
101  /* write header */
102  fwrite(&mat,sizeof(matlab),1,fp);
103  /* write name */
104  if ( name == (char *)NULL )
105  fwrite("",sizeof(char),1,fp);
106  else
107  fwrite(name,sizeof(char),(int)(mat.namlen),fp);
108  /* write actual data */
109  fwrite(x->ve,sizeof(Real),(int)(x->dim),fp);
110 
111  return x;
112 }
113 
114 /* d_save -- save double in ".mat" file for MATLAB
115  -- saves it as a row vector
116  -- returns vector to be saved */
117 double d_save(fp,x,name)
118 FILE *fp;
119 double x;
120 char *name;
121 {
122  matlab mat;
123  Real x1 = x;
124 
125  mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
126  mat.m = 1;
127  mat.n = 1;
128  mat.imag = FALSE;
129  mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
130 
131  /* write header */
132  fwrite(&mat,sizeof(matlab),1,fp);
133  /* write name */
134  if ( name == (char *)NULL )
135  fwrite("",sizeof(char),1,fp);
136  else
137  fwrite(name,sizeof(char),(int)(mat.namlen),fp);
138  /* write actual data */
139  fwrite(&x1,sizeof(Real),1,fp);
140 
141  return x;
142 }
143 
144 /* m_load -- loads in a ".mat" file variable as produced by MATLAB
145  -- matrix returned; imaginary parts ignored */
147 FILE *fp;
148 char **name;
149 {
150  MAT *A;
151  int i;
152  int m_flag, o_flag, p_flag, t_flag;
153  float f_temp;
154  Real d_temp;
155  matlab mat;
156 
157  if ( fread(&mat,sizeof(matlab),1,fp) != 1 )
158  error(E_FORMAT,"m_load");
159  if ( mat.type >= 10000 ) /* don't load a sparse matrix! */
160  error(E_FORMAT,"m_load");
161  m_flag = (mat.type/1000) % 10;
162  o_flag = (mat.type/100) % 10;
163  p_flag = (mat.type/10) % 10;
164  t_flag = (mat.type) % 10;
165  if ( m_flag != MACH_ID )
166  error(E_FORMAT,"m_load");
167  if ( t_flag != 0 )
168  error(E_FORMAT,"m_load");
169  if ( p_flag != DOUBLE_PREC && p_flag != SINGLE_PREC )
170  error(E_FORMAT,"m_load");
171  *name = (char *)malloc((unsigned)(mat.namlen)+1);
172  if ( fread(*name,sizeof(char),(unsigned)(mat.namlen),fp) == 0 )
173  error(E_FORMAT,"m_load");
174  A = m_get((unsigned)(mat.m),(unsigned)(mat.n));
175  for ( i = 0; i < A->m*A->n; i++ )
176  {
177  if ( p_flag == DOUBLE_PREC ) {
178  if (fread(&d_temp,sizeof(double),1,fp) != 1) {
179  error(E_INPUT, "m_load");
180  }
181  } else {
182  if (fread(&f_temp,sizeof(float),1,fp) != 1) {
183  error(E_INPUT, "m_load");
184  }
185  d_temp = f_temp;
186  }
187  if ( o_flag == ROW_ORDER ) {
188  A->me[i / A->n][i % A->n] = d_temp;
189  } else if ( o_flag == COL_ORDER ) {
190  A->me[i % A->m][i / A->m] = d_temp;
191  } else {
192  error(E_FORMAT,"m_load");
193  }
194  }
195 
196  if ( mat.imag ) /* skip imaginary part */
197  for ( i = 0; i < A->m*A->n; i++ )
198  {
199  if ( p_flag == DOUBLE_PREC ) {
200  if (fread(&d_temp,sizeof(double),1,fp) != 1) {
201  error(E_INPUT, "m_load");
202  }
203  } else {
204  if (fread(&f_temp,sizeof(float),1,fp) != 1) {
205  error(E_INPUT, "m_load");
206  }
207  }
208  }
209 
210  return A;
211 }
212 
static Frame * fp
Definition: code.cpp:161
#define FALSE
Definition: err.c:56
#define E_INPUT
Definition: err.h:101
#define error(err_num, fn_name)
Definition: err.h:73
#define E_FORMAT
Definition: err.h:100
#define E_NULL
Definition: err.h:102
#define Real
Definition: machine.h:189
VEC * v_save(FILE *fp, VEC *x, char *name)
Definition: matlab.c:85
MAT * m_load(FILE *fp, char **name)
Definition: matlab.c:146
MAT * m_save(FILE *fp, MAT *A, char *name)
Definition: matlab.c:44
static char rcsid[]
Definition: matlab.c:40
double d_save(FILE *fp, double x, char *name)
Definition: matlab.c:117
#define COL_ORDER
Definition: matlab.h:54
#define SINGLE_PREC
Definition: matlab.h:58
#define ROW_ORDER
Definition: matlab.h:55
#define DOUBLE_PREC
Definition: matlab.h:57
#define ORDER
Definition: matlab.h:68
#define MACH_ID
Definition: matlab.h:65
#define PRECISION
Definition: matlab.h:71
#define i
Definition: md1redef.h:12
char * name
Definition: init.cpp:16
#define A(i)
Definition: multisplit.cpp:63
size_t j
MAT * m_get(int, int)
Definition: memory.c:36
#define NULL
Definition: sptree.h:16
Definition: matrix.h:73
Definition: matrix.h:67
u_int dim
Definition: matrix.h:68
Real * ve
Definition: matrix.h:69
Definition: matlab.h:35
long namlen
Definition: matlab.h:40
long imag
Definition: matlab.h:39
long n
Definition: matlab.h:38
long type
Definition: matlab.h:36
long m
Definition: matlab.h:37