-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCMatlab.h
233 lines (219 loc) · 7.49 KB
/
CMatlab.h
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
224
225
226
227
228
229
230
231
232
233
/*This file contains an abstract base class CMatinterface which when implemented allows an object to load itself from MATLAB and save itselve to MATLAB. This is only compiled if _NDLMATLAB is defined. It requires MATLAB libraries to compile.
20/10/2005 Included William V. Baxter's modifications to allow compilation under MSVC.*/
#ifndef CMATLAB_H
#define CMATLAB_H
#ifdef _NDLMATLAB
#include "ndlexceptions.h"
#include "mex.h"
#include "mat.h"
using namespace std;
// An abstract base class which enables loading and saving of a class to MATLAB.
class CMatInterface
{
public:
virtual mxArray* toMxArray() const=0;
virtual void fromMxArray(const mxArray* matlabArray)=0;
void readMatlabFile(const string fileName, const string variableName)
{
MATFile* matFile = matOpen(fileName.c_str(), "r");
if(matFile==NULL)
throw ndlexceptions::FileReadError(fileName);
mxArray* matlabArray = matGetVariable(matFile, variableName.c_str());
if(matlabArray==NULL)
throw ndlexceptions::FileFormatError(fileName);
fromMxArray(matlabArray);
mxDestroyArray(matlabArray);
if(matClose(matFile) !=0 )
throw ndlexceptions::FileReadError(fileName);
}
void updateMatlabFile(string fileName, const string variableName) const
{
MATFile* matFile = matOpen(fileName.c_str(), "u");
if(matFile==NULL)
throw ndlexceptions::FileWriteError(fileName);
mxArray* matlabArray = toMxArray();
matPutVariable(matFile, variableName.c_str(), matlabArray);
mxDestroyArray(matlabArray);
if(matClose(matFile) !=0 )
throw ndlexceptions::FileWriteError(fileName);
}
void writeMatlabFile(const string fileName, const string variableName) const
{
MATFile* matFile = matOpen(fileName.c_str(), "w");
if(matFile==NULL)
throw ndlexceptions::FileWriteError(fileName);
mxArray* matlabArray = toMxArray();
matPutVariable(matFile, variableName.c_str(), matlabArray);
mxDestroyArray(matlabArray);
if(matClose(matFile) !=0 )
throw ndlexceptions::FileWriteError(fileName);
}
mxArray* convertMxArray(const bool val) const
{
int dims[1];
dims[0] = 1;
mxArray* matlabArray = mxCreateNumericArray(1, dims, mxDOUBLE_CLASS, mxREAL);
double* matlabVals = mxGetPr(matlabArray);
if(val)
matlabVals[0] = 1.0;
else
matlabVals[0] = 0.0;
return matlabArray;
}
mxArray* convertMxArray(const double val) const
{
int dims[1];
dims[0] = 1;
mxArray* matlabArray = mxCreateNumericArray(1, dims, mxDOUBLE_CLASS, mxREAL);
double* matlabVals = mxGetPr(matlabArray);
matlabVals[0] = val;
return matlabArray;
}
mxArray* convertMxArray(const string val) const
{
return mxCreateString(val.c_str());
}
mxArray* convertMxArray(const vector<int> vals) const
{
int dims[2];
dims[0]=vals.size();
dims[1] = 1;
mxArray* matlabArray = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
double* matlabVals = mxGetPr(matlabArray);
for(int i=0; i<vals.size(); i++)
{
matlabVals[i]=(double)vals[i];
}
return matlabArray;
}
int mxArrayToInt(const mxArray* matlabArray) const
{
int val = 0;
mxClassID classID = mxGetClassID(matlabArray);
if(classID==mxDOUBLE_CLASS)
{
assert(mxGetN(matlabArray)==1);
assert(mxGetM(matlabArray)==1);
double* valD = mxGetPr(matlabArray);
val = (int)valD[0];
}
else
throw ndlexceptions::NotImplementedError("Conversion of this type to int not yet supported.");
return val;
}
double mxArrayToDouble(const mxArray* matlabArray) const
{
double val = 0;
mxClassID classID = mxGetClassID(matlabArray);
if(classID==mxDOUBLE_CLASS)
{
assert(mxGetN(matlabArray)==1);
assert(mxGetM(matlabArray)==1);
double* valD = mxGetPr(matlabArray);
val = valD[0];
}
else
throw ndlexceptions::NotImplementedError("Conversion of this type to double not yet supported.");
return val;
}
string mxArrayToString(const mxArray* matlabArray) const
{
vector<char> charVal;
int buflen;
mxClassID classID = mxGetClassID(matlabArray);
if(classID == mxCHAR_CLASS)
{
buflen=mxGetNumberOfElements(matlabArray)*sizeof(char) + 1;
charVal.resize(buflen);
int ret = mxGetString(matlabArray, &charVal[0], buflen);
}
else
throw ndlexceptions::NotImplementedError("Conversion of this type to string not yet supported.");
string val(&charVal[0]);
return val;
}
vector<int> mxArrayToVectorInt(const mxArray* matlabArray) const
{
vector<int> val;
mxClassID classID = mxGetClassID(matlabArray);
if(classID == mxDOUBLE_CLASS)
{
int length=mxGetNumberOfElements(matlabArray);
double* valD = mxGetPr(matlabArray);
for(int i=0; i<length; i++)
val.push_back((int)valD[i]);
}
else
throw ndlexceptions::NotImplementedError("Conversion of this type to vector<int> not yet supported.");
return val;
}
bool mxArrayToBool(const mxArray* matlabArray) const
{
bool val;
mxClassID classID = mxGetClassID(matlabArray);
if(classID==mxDOUBLE_CLASS)
{
assert(mxGetN(matlabArray)==1);
assert(mxGetM(matlabArray)==1);
double* valD = mxGetPr(matlabArray);
val = valD[0];
if(val!=0)
val=true;
else
val=false;
}
else
throw ndlexceptions::NotImplementedError("Conversion of this type to bool not yet supported.");
return false;
}
mxArray* mxArrayExtractMxArrayField(const mxArray* matlabArray, const string fieldName, const int index=0) const
{
const char* fName = fieldName.c_str();
if(mxGetClassID(matlabArray) != mxSTRUCT_CLASS)
throw ndlexceptions::Error("mxArray is not a structure.");
mxArray* fieldPtr = mxGetField(matlabArray, index, fName);
return fieldPtr;
}
int mxArrayExtractIntField(const mxArray* matlabArray, const string fieldName, const int index=0) const
{
mxArray* fieldPtr = mxArrayExtractMxArrayField(matlabArray, fieldName, index);
if(fieldPtr==NULL)
throw ndlexceptions::Error("Requested field does not exist");
return mxArrayToInt(fieldPtr);
}
double mxArrayExtractDoubleField(const mxArray* matlabArray, const string fieldName, const int index=0) const
{
mxArray* fieldPtr = mxArrayExtractMxArrayField(matlabArray, fieldName, index);
if(fieldPtr==NULL)
throw ndlexceptions::Error("Requested field does not exist");
return mxArrayToDouble(fieldPtr);
}
string mxArrayExtractStringField(const mxArray* matlabArray, const string fieldName, const int index=0) const
{
mxArray* fieldPtr = mxArrayExtractMxArrayField(matlabArray, fieldName, index);
if(fieldPtr==NULL)
throw ndlexceptions::Error("Requested field does not exist");
return mxArrayToString(fieldPtr);
}
vector<int> mxArrayExtractVectorIntField(const mxArray* matlabArray, const string fieldName, const int index=0) const
{
mxArray* fieldPtr = mxArrayExtractMxArrayField(matlabArray, fieldName, index);
if(fieldPtr==NULL)
throw ndlexceptions::Error("Requested field does not exist");
return mxArrayToVectorInt(fieldPtr);
}
bool mxArrayExtractBoolField(const mxArray* matlabArray, const string fieldName, const int index=0)
{
mxArray* fieldPtr = mxArrayExtractMxArrayField(matlabArray, fieldName, index);
if(fieldPtr==NULL)
throw ndlexceptions::Error("Requested field does not exist");
return mxArrayToBool(fieldPtr);
}
};
#else /* not _NDLMATLAB */
class CMatInterface {
public:
private:
};
#endif
#endif /* not CMATLAB_H*/