Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
gmatlabdatafile.c
Go to the documentation of this file.
1 /*****************************************************************************
2 // File: gMATLABDataFile.c
3 // Contents: Functions for writing in .mat files (matlab) version 4.0.
4 // Author: G. A. Borges.
5 *****************************************************************************/
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "gmatlabdatafile.h"
12 
13 typedef struct{
14  long type;
15  long mrows;
16  long ncols;
17  long imagf;
18  long namlen;
20 
21 int gMATLABDataFile_OpenWrite(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, char *filename, char *dirname)
22 {
23  pGMatlabDataFileConfig->FileName[0] = '\0';
24  if(dirname!=NULL)
25  sprintf(pGMatlabDataFileConfig->FileName,"%s",dirname);
26  strcat(pGMatlabDataFileConfig->FileName,filename);
27  pGMatlabDataFileConfig->FlagStillNotSaved = TRUE;
28 
29  pGMatlabDataFileConfig->fp = fopen(pGMatlabDataFileConfig->FileName, "w");
30  if(pGMatlabDataFileConfig->fp==NULL){
31  printf("\n pGMatlabDataFileConfig->fp==NULL: %s",pGMatlabDataFileConfig->FileName);
32  return FALSE;
33  }
34  rewind(pGMatlabDataFileConfig->fp);
35 
36  return TRUE;
37 
38 }
39 
40 int gMATLABDataFile_OpenRead(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, char *filename, char *dirname)
41 {
42  pGMatlabDataFileConfig->FileName[0] = '\0';
43  if(dirname!=NULL)
44  sprintf(pGMatlabDataFileConfig->FileName,"%s",dirname);
45  strcat(pGMatlabDataFileConfig->FileName,filename);
46  pGMatlabDataFileConfig->FlagStillNotSaved = TRUE;
47 
48  pGMatlabDataFileConfig->fp = fopen(pGMatlabDataFileConfig->FileName, "r");
49  if(pGMatlabDataFileConfig->fp==NULL){
50  return FALSE;
51  }
52  rewind(pGMatlabDataFileConfig->fp);
53 
54  return TRUE;
55 
56 }
57 
58 void gMATLABDataFile_Close(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig)
59 {
60  fclose(pGMatlabDataFileConfig->fp);
61 
62 }
63 
64 int gMATLABDataFile_SaveVector(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, const char *varname, double *v, long nlin)
65 {
66  MATLAB_DATAHEAD DataHead;
67 
68  DataHead.type = (long)(0);
69  DataHead.mrows = (long)(nlin);
70  DataHead.ncols = (long)(1);
71  DataHead.imagf = (long)(0);
72  DataHead.namlen = (long)(strlen(varname)+1);
73 
74  if(! fwrite(&DataHead, sizeof(MATLAB_DATAHEAD), 1, pGMatlabDataFileConfig->fp) ){
75  return FALSE;
76  }
77 
78  if(! fwrite(varname, sizeof(char), (strlen(varname)+1), pGMatlabDataFileConfig->fp)){
79  return FALSE;
80  }
81 
82  if(! fwrite(v, sizeof(double), nlin, pGMatlabDataFileConfig->fp)){
83  return FALSE;
84  }
85 
86  pGMatlabDataFileConfig->FlagStillNotSaved = FALSE;
87 
88  return(TRUE);
89 
90 }
91 
92 int gMATLABDataFile_SaveMatrix(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, const char *varname, double **m, long nlin, long ncol)
93 {
94  MATLAB_DATAHEAD DataHead;
95  int nl,nc;
96  double *vcol;
97 
98  DataHead.type = (long)(0);
99  DataHead.mrows = (long)(nlin);
100  DataHead.ncols = (long)(ncol);
101  DataHead.imagf = (long)(0);
102  DataHead.namlen = (long)(strlen(varname)+1);
103 
104  if(! fwrite(&DataHead, sizeof(MATLAB_DATAHEAD), 1, pGMatlabDataFileConfig->fp) ){
105  return FALSE;
106  }
107 
108  if(! fwrite(varname, sizeof(char), (strlen(varname)+1), pGMatlabDataFileConfig->fp)){
109  return FALSE;
110  }
111 
112  vcol = (double*)malloc(nlin*sizeof(double));
113 
114  for(nc=0;nc<ncol;++nc){
115  for(nl=0;nl<nlin;++nl){
116  vcol[nl] = m[nl][nc];
117  }
118  if(! fwrite(vcol, sizeof(double), nlin, pGMatlabDataFileConfig->fp)){
119  free(vcol);
120  return FALSE;
121  }
122  }
123  free(vcol);
124 
125  pGMatlabDataFileConfig->FlagStillNotSaved = FALSE;
126 
127  return(TRUE);
128 
129 }
int gMATLABDataFile_SaveVector(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, const char *varname, double *v, long nlin)
void gMATLABDataFile_Close(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig)
int gMATLABDataFile_OpenWrite(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, char *filename, char *dirname)
int gMATLABDataFile_SaveMatrix(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, const char *varname, double **m, long nlin, long ncol)
#define FALSE
int gMATLABDataFile_OpenRead(PGMATLABDATAFILECONFIG pGMatlabDataFileConfig, char *filename, char *dirname)
#define TRUE
struct MATLAB_DATAHEAD * PMATLAB_DATAHEAD