-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatFile.h
135 lines (102 loc) · 4.13 KB
/
MatFile.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
#pragma once
#ifndef MATFILE_H_INCLUDED
#define MATFILE_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
namespace MW {
// These are the original header files from the MathWorks.
#include "mat.h"
#include "matrix.h"
const mwSize SCALAR_SIZE[2] = { static_cast<mwSize>(1), static_cast<mwSize>(1)};
}
#include "typeHelper.h"
class MATFile {
/* Describes some properties of the MAT file. Note that these are only properties that
were explicitly requested. If you open the file with 'u', for example, writes will mimic the
locale, compression, and version information of the existing data without setting these flags. */
enum MATFILE_MODE {
READABLE = 1,
WRITABLE = 2,
V4_OR_OLDER = 4, //Opened with w4, which allows saving for very old versions of Matlab (<4)
LOCAL_VARCHAR = 8, //Opened with wL, which uses current locale instead of unicode for variable names
COMPRESSED = 16, //Opened with wz, which compresses the data (a good idea!)
BIGVARS = 32 //Opened with w7.3, which uses the newer HDF format that can store variables > 2 Gb
};
public:
MATFile(const std::string& _filename, const std::string& _mode);
~MATFile();
FILE * getFP();
void close(void);
bool isReadable() const { return(mode & READABLE); }
bool isWritable() const { return(mode & WRITABLE); }
MW::mxArray* rawGetVar(const std::string& varname);
MW::mxArray* rawGetInfo(const std::string& varname);
template <typename Scalar>
void putScalar(const std::string& varname, const Scalar value, bool asGlobal = false) {
const MW::mwSize scalar[2] = {
static_cast<MW::mwSize>(1),
static_cast<MW::mwSize>(1)
};
MW::mxArray* newval = MW::mxCreateNumericArray(static_cast<MW::mwSize>(2),
scalar,
typeHelper<Scalar>(),
static_cast<MW::mxComplexity>(false));
if(newval) {
Scalar* data = static_cast<Scalar*>(MW::mxGetData(newval));
data[0] = value;
}
check_put_and_dealloc(varname, newval, asGlobal);
return;
}
template <typename Scalar>
void putArray(const std::string& varname, const Scalar* values,
MW::mwSize ndims, const MW::mwSize* dims, bool asGlobal = false) {
MW::mxArray* newval = MW::mxCreateNumericArray(ndims,
dims,
typeHelper<Scalar>(),
static_cast<MW::mxComplexity>(false));
MW::mwSize n =1;
for(MW::mwSize i=0; i < ndims; i++) {
n*= dims[i];
}
if(newval) {
Scalar* data = static_cast<Scalar*>(MW::mxGetData(newval));
for(MW::mwSize i=0; i < n; i++) {
data[i] = values[i];
}
}
check_put_and_dealloc(varname, newval, asGlobal);
return;
}
void rmVar(const std::string& varname);
std::vector<std::string> getDir();
protected:
MW::MATFile *mfp;
int mode;
std::string filename;
void check_put_and_dealloc(const std::string& varname, MW::mxArray* newval, bool asGlobal);
};
/* Specializations for MATFile::putScalar() */
template <>
void MATFile::putScalar<bool>(const std::string& varname,
const bool value, bool
asGlobal);
template <>
void MATFile::putScalar<const char*>(const std::string& varname,
const char* value,
bool asGlobal);
template <>
void MATFile::putScalar<double>(const std::string& varname,
const double value,
bool asGlobal);
template <>
void MATFile::putScalar<std::string>(const std::string& varname,
const std::string value,
bool asGlobal);
template<>
void MATFile::putArray<bool>(const std::string& varname, const bool* values,
MW::mwSize ndims, const MW::mwSize* dims,
bool asGlobal);
#endif /* defined(____MatFile__) */