MatlabWorkspace.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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& 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. // Q: Won't this be incorrect for integers?
  59. copy(M.data(),M.data()+M.size(),mxGetPr(mx_data));
  60. return *this;
  61. }
  62. template <typename ScalarM>
  63. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save(
  64. const std::vector<std::vector<ScalarM> > & vM,
  65. const std::string & name)
  66. {
  67. Eigen::MatrixXd M;
  68. list_to_matrix(vM,M);
  69. return this->save(M,name);
  70. }
  71. template <typename ScalarV>
  72. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save(
  73. const std::vector<ScalarV> & vV,
  74. const std::string & name)
  75. {
  76. Eigen::MatrixXd V;
  77. list_to_matrix(vV,V);
  78. return this->save(V,name);
  79. }
  80. template <typename DerivedM>
  81. IGL_INLINE igl::MatlabWorkspace&
  82. igl::MatlabWorkspace::save_index(
  83. const Eigen::PlainObjectBase<DerivedM>& M,
  84. const std::string & name)
  85. {
  86. DerivedM Mp1 = M;
  87. Mp1.array() += 1;
  88. return this->save(Mp1,name);
  89. }
  90. template <typename ScalarM>
  91. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  92. const std::vector<std::vector<ScalarM> > & vM,
  93. const std::string & name)
  94. {
  95. Eigen::MatrixXd M;
  96. list_to_matrix(vM,M);
  97. return this->save_index(M,name);
  98. }
  99. template <typename ScalarV>
  100. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  101. const std::vector<ScalarV> & vV,
  102. const std::string & name)
  103. {
  104. Eigen::MatrixXd V;
  105. list_to_matrix(vV,V);
  106. return this->save_index(V,name);
  107. }
  108. //template <typename Data>
  109. //bool igl::MatlabWorkspace::save(const Data & M, const std::string & name)
  110. //{
  111. // using namespace std;
  112. // // If I don't know the type then I can't save it
  113. // cerr<<"^MatlabWorkspace::save Error: Unknown data type. "<<
  114. // name<<" not saved."<<endl;
  115. // return false;
  116. //}