MatlabWorkspace.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "igl/matlab/MatlabWorkspace.h"
  2. // IGL
  3. #include "igl/list_to_matrix.h"
  4. // MATLAB
  5. #include "mat.h"
  6. // STL
  7. #include <iostream>
  8. #include <algorithm>
  9. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace()
  10. {
  11. }
  12. IGL_INLINE igl::MatlabWorkspace::~MatlabWorkspace()
  13. {
  14. // clean up data
  15. clear();
  16. }
  17. IGL_INLINE void igl::MatlabWorkspace::clear()
  18. {
  19. for_each(data.begin(),data.end(),&mxDestroyArray);
  20. }
  21. IGL_INLINE bool igl::MatlabWorkspace::write(const std::string & path) const
  22. {
  23. using namespace std;
  24. MATFile * mat_file = matOpen(path.c_str(), "w");
  25. assert(names.size() == data.size());
  26. // loop over names and data
  27. for(int i = 0;i < (int)names.size(); i++)
  28. {
  29. // Put variable as LOCAL variable
  30. int status = matPutVariable(mat_file,names[i].c_str(), data[i]);
  31. if(status != 0)
  32. {
  33. cerr<<"^MatlabWorkspace::save Error: matPutVariable ("<<names[i]<<
  34. ") failed"<<endl;
  35. return false;
  36. }
  37. }
  38. if(matClose(mat_file) != 0)
  39. {
  40. fprintf(stderr,"Error closing file %s\n",path.c_str());
  41. return false;
  42. }
  43. return true;
  44. }
  45. // Treat everything as a double
  46. template <typename DerivedM>
  47. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save(
  48. const Eigen::PlainObjectBase<DerivedM>& M,
  49. const std::string & name)
  50. {
  51. using namespace std;
  52. const int m = M.rows();
  53. const int n = M.cols();
  54. mxArray * mx_data = mxCreateDoubleMatrix(m,n,mxREAL);
  55. data.push_back(mx_data);
  56. names.push_back(name);
  57. // Copy data immediately
  58. copy(M.data(),M.data()+M.size(),mxGetPr(mx_data));
  59. return *this;
  60. }
  61. template <typename ScalarM>
  62. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save(
  63. const std::vector<std::vector<ScalarM> > & vM,
  64. const std::string & name)
  65. {
  66. Eigen::MatrixXd M;
  67. list_to_matrix(vM,M);
  68. return this->save(M,name);
  69. }
  70. template <typename ScalarV>
  71. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save(
  72. const std::vector<ScalarV> & vV,
  73. const std::string & name)
  74. {
  75. Eigen::MatrixXd V;
  76. list_to_matrix(vV,V);
  77. return this->save(V,name);
  78. }
  79. template <typename DerivedM>
  80. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace&
  81. igl::MatlabWorkspace::save_index(
  82. const Eigen::PlainObjectBase<DerivedM>& M,
  83. const std::string & name)
  84. {
  85. DerivedM Mp1 = M;
  86. Mp1.array() += 1;
  87. return this->save(Mp1,name);
  88. }
  89. template <typename ScalarM>
  90. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  91. const std::vector<std::vector<ScalarM> > & vM,
  92. const std::string & name)
  93. {
  94. Eigen::MatrixXd M;
  95. list_to_matrix(vM,M);
  96. return this->save_index(M,name);
  97. }
  98. template <typename ScalarV>
  99. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  100. const std::vector<ScalarV> & vV,
  101. const std::string & name)
  102. {
  103. Eigen::MatrixXd V;
  104. list_to_matrix(vV,V);
  105. return this->save_index(V,name);
  106. }
  107. //template <typename Data>
  108. //bool igl::MatlabWorkspace::save(const Data & M, const std::string & name)
  109. //{
  110. // using namespace std;
  111. // // If I don't know the type then I can't save it
  112. // cerr<<"^MatlabWorkspace::save Error: Unknown data type. "<<
  113. // name<<" not saved."<<endl;
  114. // return false;
  115. //}