Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

operator/outputMatlab.hh
Go to the documentation of this file.
00001 /* matlab output function for matrices
00002  */
00003 
00004 #ifndef OperatorOutputMatlab_hh
00005 #define OperatorOutputMatlab_hh
00006 
00007 #include "basics/outputMatlab.hh"
00008 #include "sparseMatrix.hh"
00009 
00010 namespace concepts {
00011 
00012   // ********************************************** storeDenseMatrixToMatlab **
00013 
00015   static uint storeDenseMatrixMatlabCounter_ = 0;
00016   static uint storeSparseMatrixMatlabCounter_ = 0;
00017 
00029   template<class F>
00030   bool storeDenseMatrixToMatlab(F& matrix, const uint nofRows, 
00031         const uint nofCols, const std::string filename,
00032         std::string name = "", bool append = false) {
00033 
00034     // Mode, new or append
00035     std::ios_base::openmode mode = std::ofstream::out;
00036     if (append) mode = mode | std::ofstream::app;
00037 
00038     std::ofstream ofs(filename.c_str(), mode);
00039     if (!ofs.is_open()) {
00040       std::cerr << "Could not open file '" << filename << "'" << std::endl;
00041       return false;
00042     }
00043 
00044     storeDenseMatrixToMatlab(matrix, nofRows, nofCols, ofs, name);
00045     return true;
00046   }
00047 
00048 
00059   template<class F>
00060     void storeDenseMatrixToMatlab(F& matrix, const uint nofRows, 
00061         const uint nofCols, std::ostream& ofs,
00062         std::string name = "") {
00063       if (name.empty()) {
00064         std::stringstream s;
00065         s << "MatDense_" << storeDenseMatrixMatlabCounter_++;
00066         name = s.str();
00067       }
00068       std::string offset(name.size() + 4, ' ');
00069       ofs << name << " = [" << std::setprecision(16);
00070 
00071       for (uint i = 0; i < nofRows; ++i) {
00072         for (uint j = 0; j < nofCols; ++j) {
00073           ofs << OutputMatlab<typename F::value_type>(matrix(i,j));
00074           if (j == nofCols-1) {
00075             if (i < nofRows-1)
00076               ofs << std::endl << offset;
00077           } else ofs << ' ';
00078         }
00079       }
00080       ofs << "];" << std::endl;
00081     }
00082 
00083 
00095   template<class F>
00096   void storeSparseMatrixToOctave(SparseMatrix<F>& matrix, std::ostream& ofs,
00097                                  std::string name = "")
00098   {
00099     if (name.empty()) {
00100       std::stringstream s;
00101       s << "MatSparse_" << storeSparseMatrixMatlabCounter_++;
00102       name = s.str();
00103     }
00104     std::string offset(name.size() + 4, ' ');
00105     ofs << name << " = zeros(" << matrix.nofRows() << ", " << matrix.nofCols()
00106         << ");" << std::endl;
00107     ofs << "zzz = [" << std::endl << std::setprecision(16);
00108     matrix.m()->outputMatlab(ofs);
00109     ofs << "];" << std::endl;
00110     ofs << "for i__=1:size(zzz,1)" << std::endl
00111         << "  " << name << "(zzz(i__,1), zzz(i__,2)) = zzz(i__,3);"<< std::endl
00112         << "end" << std::endl
00113         << "clear zzz" << std::endl;
00114   }
00115 
00116 } // namespace concepts
00117 
00118 #endif // OperatorOutputMatlab_hh

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)