Index: src/LinearAlgebra/MatrixContent.cpp
===================================================================
--- src/LinearAlgebra/MatrixContent.cpp	(revision 0e4aa4d2a8b0ce6daba3c91d5905d1a870b8d520)
+++ src/LinearAlgebra/MatrixContent.cpp	(revision 6f68d6fb6d9ac0188a952835078622c964fe52dc)
@@ -44,5 +44,6 @@
 MatrixContent::MatrixContent(size_t _rows, size_t _columns) :
     rows(_rows),
-    columns(_columns)
+    columns(_columns),
+    free_content_on_exit(true)
 {
   content = gsl_matrix_calloc(rows, columns);
@@ -58,5 +59,6 @@
 MatrixContent::MatrixContent(size_t _rows, size_t _columns, MatrixBaseCase) :
     rows(_rows),
-    columns(_columns)
+    columns(_columns),
+    free_content_on_exit(true)
 {}
 
@@ -68,5 +70,6 @@
 MatrixContent::MatrixContent(size_t _rows, size_t _columns, const double *src) :
     rows(_rows),
-    columns(_columns)
+    columns(_columns),
+    free_content_on_exit(true)
 {
   content = gsl_matrix_calloc(rows, columns);
@@ -84,12 +87,67 @@
 }
 
+/** Constructor that parses square matrix from a stream.
+ *
+ * \note Matrix dimensions can be preparsed via
+ * MatrixContent::preparseMatrixDimensions() without harming the stream.
+ *
+ * \param &inputstream stream to parse from
+ */
+MatrixContent::MatrixContent(size_t _row, size_t _column, std::istream &inputstream) :
+  content(NULL),
+  rows(_row),
+  columns(_column),
+  free_content_on_exit(true)
+{
+   // allocate matrix and set contents
+  content = gsl_matrix_alloc(rows, columns);
+
+  size_t row = 0;
+  do {
+    std::string line;
+    getline(inputstream, line);
+    //std::cout << line << std::endl;
+    std::stringstream parseline(line);
+    // skip comments
+    if ((parseline.peek() == '#') || (parseline.str().empty()))
+      continue;
+    // break on empty lines
+    if (parseline.peek() == '\n')
+      break;
+
+    // parse line with values
+    std::vector<double> line_of_values;
+    do {
+      double value;
+      parseline >> value >> ws;
+      line_of_values.push_back(value);
+    } while (parseline.good());
+
+    // check number of columns parsed
+    ASSERT(line_of_values.size() == columns,
+        "MatrixContent::MatrixContent() - row "
+        +toString(row)+" has a different number of columns "
+        +toString(line_of_values.size())+" than others before "
+        +toString(columns)+".");
+
+    for (size_t column = 0; column < columns; ++column)
+      set(row, column, line_of_values[column]);
+    ++row;
+  } while (inputstream.good());
+  // check number of rows parsed
+  ASSERT(row == rows,
+      "MatrixContent::MatrixContent() - insufficent number of rows "
+      +toString(row)+" < "+toString(rows)+" read.");
+}
+
+
 /** Constructor for class MatrixContent.
- * We embed the given gls_matrix pointer within this class and set it to NULL
- * afterwards.
- * \param *src source gsl_matrix vector to embed within this class
+ *
+ * \param *src source gsl_matrix vector to copy and put into this class
  */
 MatrixContent::MatrixContent(gsl_matrix *&src) :
     rows(src->size1),
-    columns(src->size2)
+    columns(src->size2),
+    free_content_on_exit(true)
 {
   content = gsl_matrix_alloc(src->size1, src->size2);
@@ -104,5 +162,6 @@
 MatrixContent::MatrixContent(const MatrixContent &src) :
     rows(src.rows),
-    columns(src.columns)
+    columns(src.columns),
+    free_content_on_exit(true)
 {
   content = gsl_matrix_alloc(src.rows, src.columns);
@@ -115,5 +174,6 @@
 MatrixContent::MatrixContent(const MatrixContent *src) :
     rows(src->rows),
-    columns(src->columns)
+    columns(src->columns),
+    free_content_on_exit(true)
 {
   ASSERT(src != NULL, "MatrixContent::MatrixContent - pointer to source matrix is NULL!");
@@ -126,5 +186,6 @@
 MatrixContent::~MatrixContent()
 {
-  gsl_matrix_free(content);
+  if (free_content_on_exit)
+    gsl_matrix_free(content);
 }
 
@@ -607,4 +668,75 @@
 };
 
+/** Preparses a matrix in an input stream for row and column count.
+ *
+ *  \note This does not change the get position within the stream.
+ *
+ * @param inputstream to parse matrix from
+ * @return pair with (row, column)
+ */
+std::pair<size_t, size_t> MatrixContent::preparseMatrixDimensions(std::istream &inputstream)
+{
+  const std::streampos initial_position(inputstream.tellg());
+//  std::cout << "We are at position "
+//      +toString((int)inputstream.tellg())+" from start of stream." << std::endl;
+  size_t rows = 0;
+  size_t columns = 0;
+  do {
+    std::string line;
+    getline(inputstream, line);
+    std::stringstream parseline(line);
+    // skip comments
+    if ((parseline.peek() == '#') || (line.empty()))
+      continue;
+    // break on empty lines
+    if (parseline.peek() == '\n')
+      break;
+
+    // parse line with values
+    std::vector<double> line_of_values;
+    do {
+      double value;
+      parseline >> value >> ws;
+      line_of_values.push_back(value);
+    } while (parseline.good());
+
+    // set matrixdimension if first line
+    if (columns == 0) {
+      columns = line_of_values.size();
+    } else { // otherwise check
+      ASSERT(columns == line_of_values.size(),
+          "MatrixContent::MatrixContent() - row "
+          +toString(rows)+" has a different number of columns "
+          +toString(line_of_values.size())+" than this matrix has "
+          +toString(columns)+".");
+    }
+    ++rows;
+  } while (inputstream.good());
+  // clear end of file flags
+  inputstream.clear();
+
+  // reset to initial position
+  inputstream.seekg(initial_position);
+//  std::cout << "We are again at position "
+//      +toString((int)inputstream.tellg())+" from start of stream." << std::endl;
+
+  return make_pair(rows, columns);
+}
+
+/** Writes matrix content to ostream in such a manner that it can be re-parsed
+ * by the code in the constructor.
+ *
+ * @param ost stream to write to
+ */
+void MatrixContent::write(std::ostream &ost) const
+{
+  for (size_t row = 0; row < rows; ++row) {
+    for (size_t column = 0; column < columns; ++column)
+      ost << at(row, column) << "\t";
+    ost << std::endl;
+  }
+}
+
+
 /* ============================= Operators =============================== */
 
Index: src/LinearAlgebra/MatrixContent.hpp
===================================================================
--- src/LinearAlgebra/MatrixContent.hpp	(revision 0e4aa4d2a8b0ce6daba3c91d5905d1a870b8d520)
+++ src/LinearAlgebra/MatrixContent.hpp	(revision 6f68d6fb6d9ac0188a952835078622c964fe52dc)
@@ -71,4 +71,5 @@
   MatrixContent(size_t _rows, size_t _columns, MatrixBaseCase);
   MatrixContent(size_t rows, size_t columns, const double *src);
+  MatrixContent(size_t rows, size_t columns, std::istream &inputstream);
   MatrixContent(gsl_matrix *&src);
   MatrixContent(const MatrixContent &src);
@@ -124,4 +125,7 @@
   VectorContent *getDiagonalVector() const;
 
+  static std::pair<size_t, size_t> preparseMatrixDimensions(std::istream &inputstream);
+  void write(std::ostream &ost) const;
+
 protected:
   double *Pointer(size_t m, size_t n);
@@ -133,4 +137,5 @@
 
 private:
+  bool free_content_on_exit;
 };
 
Index: src/LinearAlgebra/unittests/MatrixContentUnitTest.cpp
===================================================================
--- src/LinearAlgebra/unittests/MatrixContentUnitTest.cpp	(revision 0e4aa4d2a8b0ce6daba3c91d5905d1a870b8d520)
+++ src/LinearAlgebra/unittests/MatrixContentUnitTest.cpp	(revision 6f68d6fb6d9ac0188a952835078622c964fe52dc)
@@ -266,2 +266,29 @@
   CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
 };
+
+
+/** Unit test for reading from and writing matrix to stream
+ *
+ */
+void MatrixContentTest::ReadWriteTest()
+{
+  // set up matrix
+  for (int i=0;i<4;i++)
+    for (int j=0;j<3;j++)
+      m->set(i,j, i*3+j );
+
+  // write to stream
+  std::stringstream matrixstream;
+  m->write(matrixstream);
+
+  // parse in dimensions and check
+  std::pair<size_t, size_t> matrixdimensions = MatrixContent::preparseMatrixDimensions(matrixstream);
+  CPPUNIT_ASSERT_EQUAL( (size_t)4, matrixdimensions.first );
+  CPPUNIT_ASSERT_EQUAL( (size_t)3, matrixdimensions.second );
+  // parse in matrix and check
+  MatrixContent* n = new MatrixContent(4,3, matrixstream);
+  CPPUNIT_ASSERT_EQUAL( *m, *n );
+
+  // free matrix
+  delete n;
+}
Index: src/LinearAlgebra/unittests/MatrixContentUnitTest.hpp
===================================================================
--- src/LinearAlgebra/unittests/MatrixContentUnitTest.hpp	(revision 0e4aa4d2a8b0ce6daba3c91d5905d1a870b8d520)
+++ src/LinearAlgebra/unittests/MatrixContentUnitTest.hpp	(revision 6f68d6fb6d9ac0188a952835078622c964fe52dc)
@@ -29,4 +29,5 @@
     CPPUNIT_TEST (ExchangeTest );
     CPPUNIT_TEST (PropertiesTest );
+    CPPUNIT_TEST (ReadWriteTest );
     CPPUNIT_TEST_SUITE_END();
 
@@ -40,4 +41,5 @@
     void ExchangeTest();
     void PropertiesTest();
+    void ReadWriteTest();
 
 private:
