-
Notifications
You must be signed in to change notification settings - Fork 2
/
mat_read.cpp
112 lines (108 loc) · 3.2 KB
/
mat_read.cpp
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
#include<mat_read.h>
complex<double>*** ReadComplexMxArray3D(const char *filename,const char *variable)
{
// read .mat convert to c++ matrix
MATFile *pmatFile = NULL;
mxArray *pMxArray = NULL;
pmatFile = matOpen(filename , "r");
if(pmatFile == NULL)
{
cout<<"mat file can not open"<<endl;
exit(1);
}
pMxArray = matGetVariable(pmatFile, variable);
const mwSize* array_dim = mxGetDimensions(pMxArray); // return Pointer of first element in the dimensions array
mwSize num_dim = mxGetNumberOfDimensions(pMxArray); // return number of dimensions in the specified mxArray(MATLAB Array)
size_t m = array_dim[0];
size_t n = array_dim[1];
size_t d = array_dim[2];
complex<double>* ptr;
ptr = (complex<double> *)mxGetComplexDoubles(pMxArray);
complex<double> *** c_S;
c_S = new complex<double>**[m];
for(int i=0;i<m;i++)
{
c_S[i] = new complex<double>*[n];
for(int j=0;j<n;j++)
{
c_S[i][j] = new complex<double>[d];
}
}
for(size_t k=0;k<d;k++)
{
for(size_t j=0;j<n;j++)
{
for(size_t i=0;i<m;i++)
{
c_S[i][j][k] = ptr[k*m*n + j*m + i ];
}
}
}
return c_S;
}
double ** ReadDoubleMxArray(const char *filename, const char *variable)
{
MATFile *pmatFile = NULL;
mxArray *pMxArray = NULL;
pmatFile = matOpen(filename , "r");
if(pmatFile == NULL)
{
cout<<"mat file can not open"<<endl;
exit(1);
}
pMxArray = matGetVariable(pmatFile, variable);
const mwSize* array_dim = mxGetDimensions(pMxArray); // return Pointer of first element in the dimensions array
size_t m = array_dim[0];
size_t n = array_dim[1];
double *ptr;
ptr = (double *)mxGetDoubles(pMxArray);
double **c_data;
c_data = new double *[m];
for(size_t i=0;i<m;i++)
{
c_data[i] = new double[n];
}
for(size_t j=0;j<n;j++)
{
for(size_t i=0;i<m;i++)
{
c_data[i][j] = ptr[j*m + i ];
}
}
return c_data;
}
dim3D matGetDim3D(const char *filename,const char *variable)
{
// get .mat data dimensions
MATFile *pmatFile = NULL;
mxArray *pMxArray = NULL;
pmatFile = matOpen(filename , "r");
pMxArray = matGetVariable(pmatFile, variable);
dim3D arraydim;
const size_t* array_dim = (size_t *)mxGetDimensions(pMxArray);
mwSize num_dim = mxGetNumberOfDimensions(pMxArray); // return number of dimensions in the specified mxArray(MATLAB Array)
arraydim.m = array_dim[0];
arraydim.n = array_dim[1];
if(num_dim == 3)
{
arraydim.d = array_dim[2];
}else{
arraydim.d = 1;
}
return arraydim;
}
void FreeComplexArray3D(complex<double> ***arraydata, dim3D arraydim)
{
// free c++ complexmatrix memory
size_t m,n,d;
m = arraydim.m; n = arraydim.n; d = arraydim.d;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
delete[] arraydata[i][j];
}
delete[] arraydata[i];
}
delete[] arraydata;
}