MatLogger2  1.0.0
Library for logging of numeric data to HDF5 MAT-files, which is RT-safe and multithreaded.
matio_backend.cpp
Go to the documentation of this file.
1 #include "matio_backend.h"
2 
3 using namespace XBot::matlogger2;
4 
5 extern "C" Backend * create_instance()
6 {
7  return new MatioBackend;
8 }
9 
10 bool MatioBackend::init(std::string logger_name,
11  bool enable_compression)
12 {
13  _mat_file = Mat_CreateVer(logger_name.c_str(),
14  nullptr,
15  MAT_FT_MAT73);
16 
17  _compression = enable_compression ? MAT_COMPRESSION_ZLIB : MAT_COMPRESSION_NONE;
18 
19  return _mat_file;
20 }
21 
22 bool MatioBackend::write(const char* name, const double* data, int rows, int cols, int slices)
23 {
24  int n_dims = slices == 1 ? 2 : 3;
25  std::size_t dims[3];
26  dims[0] = rows;
27  dims[1] = cols;
28  dims[2] = slices;
29 
30  auto * mat_var = Mat_VarCreate(name,
31  MAT_C_DOUBLE,
32  MAT_T_DOUBLE,
33  n_dims,
34  dims,
35  (void *)data,
36  MAT_F_DONT_COPY_DATA);
37 
38  int dim_append = slices == 1 ? 2 : 3;
39  int ret = Mat_VarWriteAppend(_mat_file,
40  mat_var,
41  _compression,
42  dim_append); // TBD compression from
43  // user
44 
45  if(ret != 0)
46  {
47  fprintf(stderr, "Mat_VarWriteAppend failed with code %d \n", ret);
48  }
49 
50  // free mat variable
51  Mat_VarFree(mat_var);
52 
53  return ret == 0;
54 
55 }
56 
58 {
59  return 0 == Mat_Close(_mat_file);
60 }
61 
virtual bool close() override
virtual bool init(std::string logger_name, bool enable_compression) override
Backend * create_instance()
virtual bool write(const char *name, const double *data, int rows, int cols, int slices) override