matlab_format.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef IGL_MATLAB_FORMAT_H
  2. #define IGL_MATLAB_FORMAT_H
  3. #include "igl_inline.h"
  4. #include <Eigen/Core>
  5. #include <Eigen/Sparse>
  6. #include <string>
  7. namespace igl
  8. {
  9. // This is a routine to print a matrix using format suitable for pasting into
  10. // the matlab IDE
  11. //
  12. // Templates:
  13. // DerivedM e.g. derived from MatrixXd
  14. // Input:
  15. // input some matrix to be formated
  16. // name name of matrix
  17. // Returns Formated matrix
  18. //
  19. // Example:
  20. // // M := [1 2 3;4 5 6];
  21. // cout<<matlab_format(M)<<endl;
  22. // // Prints:
  23. // // [
  24. // // 1 2 3
  25. // // 4 5 6
  26. // // ];
  27. // cout<<matlab_format(M,"M")<<endl;
  28. // // Prints:
  29. // // M = [
  30. // // 1 2 3
  31. // // 4 5 6
  32. // // ];
  33. template <typename DerivedM>
  34. IGL_INLINE const Eigen::WithFormat< DerivedM > matlab_format(
  35. const Eigen::PlainObjectBase<DerivedM> & M,
  36. const std::string name = "");
  37. // Same but for sparse matrices. Print IJV format into an auxillary variable
  38. // and then print a call to sparse which will construct the sparse matrix
  39. // Example:
  40. // // S := [0 2 3;4 5 0];
  41. // cout<<matlab_format(S,"S")<<endl;
  42. // // Prints:
  43. // // SIJV = [
  44. // // 2 1 4
  45. // // 1 2 2
  46. // // 2 2 5
  47. // // 1 3 3
  48. // // ];
  49. // // S = sparse(SIJV(:,1),SIJV(:,2),SIJV(:,3));
  50. //
  51. template <typename DerivedS>
  52. IGL_INLINE const std::string matlab_format(
  53. const Eigen::SparseMatrix<DerivedS> & S,
  54. const std::string name = "");
  55. // Return just IOFormat
  56. //
  57. // Example:
  58. // // M := [1 2 3;4 5 6];
  59. // cout<<M.format(matlab_format())<<endl;
  60. // // Prints:
  61. // // [
  62. // // 1 2 3
  63. // // 4 5 6
  64. // // ];
  65. IGL_INLINE Eigen::IOFormat matlab_format();
  66. }
  67. #ifdef IGL_HEADER_ONLY
  68. # include "matlab_format.cpp"
  69. #endif
  70. #endif