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