Go to the documentation of this file.00001 #ifndef matlabinterface_hh
00002 #define matlabinterface_hh
00003
00004 #include <iostream>
00005
00006 #include "basics/exceptions.hh"
00007 #include "operator.hh"
00008
00009 #include "mat.h"
00010 #include "matrix.h"
00011
00012 namespace concepts {
00013
00014
00015 class MatlabMatfile;
00016
00017
00018
00023 class MatlabMatfileError : public ExceptionBase {
00024 public:
00029 MatlabMatfileError(const MatlabMatfile& matfile) throw();
00030
00031 virtual ~MatlabMatfileError() throw() {};
00032 protected:
00033 virtual std::ostream& info(std::ostream& os) const throw();
00034 private:
00035 std::string errorMessage_;
00036 };
00037
00038
00039
00047 class MatlabMatfile {
00048
00049 public:
00050 MatlabMatfile() : file_(0) { }
00051
00055 MatlabMatfile(const std::string& filename) : file_(0), filename_(filename) {
00056 openFile( filename, "u" );
00057
00058 if( !isOpen() ) {
00059 openFile( filename, "w" );
00060 closeFile();
00061 openFile( filename, "u" );
00062 }
00063 }
00064
00070 MatlabMatfile(const std::string& filename, const std::string& mode)
00071 : file_(0), filename_(filename) {
00072 openFile( filename, mode );
00073 }
00074
00075 inline bool isOpen() const { return (file_!=NULL); }
00076
00077 inline std::string filename() const { return filename_; }
00078
00079 virtual ~MatlabMatfile() {
00080 closeFile();
00081 }
00082
00084
00085 std::string getErrorMessage() const {
00086 return errorMessage_.str();
00087 }
00088
00104 void openFile(const std::string& filename, const std::string& mode);
00105
00106 void closeFile() {
00107 if( isOpen() ) {
00108 matClose( file_ );
00109 file_ = 0;
00110 filename_ = "";
00111 }
00112 }
00113
00121 template<typename F>
00122 int addMatrix(Matrix<F>* p_matrix, const std::string& varname);
00123
00125 void deleteMatrix(const std::string& varname) {
00126 if( isOpen() ) {
00127 int retval = matDeleteVariable(file_, varname.c_str());
00128 if( retval!=0 ){
00129 setErrorMessage_("Could not delete Variable. No such variable name.");
00130 }
00131 } else {
00132 setErrorMessage_("Could not delete Variable. No MAT-file opened.");
00133 }
00134 }
00135
00144 template<typename F>
00145 Matrix<F>* getMatrix(const std::string& varname);
00146
00148 template<typename F>
00149 SparseMatrix<F>* getSparseMatrix(const std::string& varname);
00150
00152 template<typename F>
00153 DenseMatrix<F>* getDenseMatrix(const std::string& varname);
00154
00156 template<typename F>
00157 Vector<F>* getVector(const std::string& varname) const;
00158
00160 template<typename F>
00161 F getScalar(const std::string& varname);
00162
00164 bool exists(const std::string& varname) const;
00165
00167 void assertExistence(const std::string& varname) const throw();
00168
00170 bool isReal(const std::string& varname) const;
00171
00173 bool isCmplx(const std::string& varname) const;
00174
00176 void assertCmplx(const std::string& varname) const throw();
00177
00179 bool isScalar(const std::string& varname) const;
00180
00182 uint lengthVector(const std::string& varname) const;
00183
00184 friend std::ostream& operator<<(std::ostream& out, const MatlabMatfile& mm);
00185
00186 private:
00188 MATFile* file_;
00190 std::string filename_;
00191
00194 mutable std::stringstream errorMessage_;
00195
00196 void clearErrorMessage_() const {
00197 errorMessage_.clear();
00198 errorMessage_.str("");
00199 }
00200
00201 void setErrorMessage_(const std::string& newErrorMessage) const {
00202 errorMessage_.clear();
00203 errorMessage_.str(newErrorMessage);
00204 }
00205
00206 void assertScalar_(const std::string& varname) const throw();
00207
00208 void assertVector_(const std::string& varname, uint& length) const throw();
00209
00210 template<typename F>
00211 struct NumTr{
00212 const static bool is_complex = false;
00213 };
00214
00215 template<typename F>
00216 struct NumTr< std::complex<F> >{
00217 const static bool is_complex = true;
00218 };
00219
00220 };
00221
00222 }
00223
00224 #endif