-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatread.cpp
More file actions
153 lines (115 loc) · 3.09 KB
/
Copy pathmatread.cpp
File metadata and controls
153 lines (115 loc) · 3.09 KB
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
/*
matread: read a 2D array from a text file.
Supports NaN and doubles...
Meant to be a fast replacement for matlab's load command.
Daniel Roggen, 2013-2017
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <mex.h>
#define printf mexPrintf
//extern "C" int parse(char *data,int *sx,int *sy,double **outdata,bool **outnan);
int parse(char *data,int *sx,int *sy,double **outdata,bool **outnan,int fixedcol);
#define MAXFNAME 255
void Syntax()
{
printf("matread reads a 2D array from a text file, space separated.\n");
printf("matread is meant to be a fast replacement to matlab's load.\n\n");
printf(" data = matread(filename[,numcol])\n\n");
printf(" filename: file name in which to write the matrix\n");
printf(" numcol: optional parameter indicating how many columns to create in data.\n");
printf(" numcol can be used to handle with files with lines of various sizes.\n");
printf(" if a line has less than numcol samples the remaining columns are filled with zeros.\n");
printf(" if a line has more than numcol samples these samples are lost.\n");
printf(" data: data read from the file\n");
}
void Error(char *s)
{
mexErrMsgTxt(s);
}
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{
char filename[MAXFNAME+1];
int sx,sy;
int x,y;
double r=0;
int fwerr=0;
int percent=0;
FILE *f;
int ok=false;
unsigned fsize;
char *buffer;
bool *outnan;
double *outdata;
double *mptr;
size_t n;
long long nanvar = 0xfff6ac007ffead00;
int fixedcol=0; // Zero=not fixed columns
// Check in/out args
if(nlhs!=1)
{
Syntax();
Error("Missing output argument");
}
if( !(nrhs==1 || nrhs==2) )
{
Syntax();
Error("Invalid input argument");
}
if(nrhs==2)
{
// Fixed number of columns
if(mxGetClassID(prhs[1]) != mxDOUBLE_CLASS || mxGetM(prhs[1])!=1 || mxGetN(prhs[1])!=1)
{
Syntax();
Error("Invalid number of columns");
}
// Get the number of columns
fixedcol = *mxGetPr(prhs[1]);
//printf("Fixed columns: %d\n",fixedcol);
}
if(mxGetClassID(prhs[0]) != mxCHAR_CLASS)
Error("Error: filename must be a string");
mxGetString(prhs[0],filename,MAXFNAME);
///printf("Reading %s\n",filename);
f=fopen(filename,"rb");
if(f==0)
{
Error("Error: can't open file");
}
fseek(f, 0L, SEEK_END);
fsize = ftell(f);
fseek(f, 0L, SEEK_SET);
buffer = (char*)malloc((fsize+1)*sizeof(char));
if(!buffer)
Error("Error: can't allocate enough memory");
n=fread(buffer,1,fsize,f);
if( n!=fsize )
{
free(buffer);
Error("Error: can't read the file");
}
fclose(f);
// Null terminate the data.
buffer[fsize]=0;
ok = parse(buffer,&sx,&sy,&outdata,&outnan,fixedcol);
free(buffer);
if(ok!=0)
Error("Error: can't parse file");
//printf("Read a %d x %d array from %s\n",sy,sx,filename);
plhs[0] = mxCreateDoubleMatrix(sy,sx,mxREAL);
mptr = mxGetPr(plhs[0]);
for(y=0;y<sy;y++)
{
for(x=0;x<sx;x++)
{
if(outnan[y*sx+x])
*(long long*)&mptr[x*sy+y] = nanvar;
else
mptr[x*sy+y] = outdata[y*sx+x];
}
}
free(outdata);
free(outnan);
}