matfileTutorial.cc

This tutorial shows how to export and import data from Concepts to files in Matlab's binary format.

  1. Commented Program
    1. Preparations
    2. Export data from Concepts to a matfile
    3. Import data to Concepts from a matfile
  2. Complete Source Code

Commented Program

Include files for input/output handling and matrices.

#include "matfile.hh"
#include "operator.hh"

Preparations

In the first lines two matrices are initialized.

for(uint i=0; i<matrix1.dimX(); ++i){
for(uint j=0; j<matrix1.dimY(); ++j){
matrix1(i,j) = std::complex<double>(std::rand(),std::rand());
}
}
for(uint i=0;i<std::min(matrix2.dimX(),matrix2.dimY());++i)
matrix2(i,i) = std::rand();

Export data from Concepts to a matfile

Now we want to write these two matrices into a binary Matlab file. First, we create a concepts::MatfileIO with the designated filename as string input.

concepts::MatfileIO mo("MatfileIO");

Then we use the method add to add data to the created matfile

mo.add(matrix1,"Matrix1");
mo.add(matrix2,"Matrix2");

where we specify the variable name of the data. Finally, we close the matfile.

mo.close();

Import data to Concepts from a matfile

Now we want to (re-)import the matrices from the matfile. We open the matfile

concepts::MatfileIO mi("MatfileIO");

and check the properties of the variable Matrix1.

std::cout << "exists(Matrix1): " << mi.exists("Matrix1") << std::endl;
std::cout << "isScalar(Matrix1): " << mi.isScalar("Matrix1") << std::endl;
std::cout << "isDense(Matrix1): " << mi.isDense("Matrix1") << std::endl;
std::cout << "isSparse(Matrix1): " << mi.isSparse("Matrix1") << std::endl;
std::cout << "isUint(Matrix1): " << mi.isUint("Matrix1") << std::endl;
std::cout << "isInt(Matrix1): " << mi.isInt("Matrix1") << std::endl;
std::cout << "isReal(Matrix1): " << mi.isReal("Matrix1") << std::endl;
std::cout << "isCmplx(Matrix1): " << mi.isCmplx("Matrix1") << std::endl;

We read the two matrices from the matfile making sure that they exist and have the correct properties.

if ((mi.exists("Matrix1")) && (mi.isDense("Matrix1")) && (mi.isCmplx("Matrix1")))
mi.get(matrix1_input,"Matrix1");
if ((mi.exists("Matrix2")) && (mi.isSparse("Matrix2")) && (mi.isReal("Matrix2")))
mi.get(matrix2_input,"Matrix2");

We close the matfile.

mi.close();

Finally, we print the exported and imported matrices on the screen.

std::cout << std::endl << "Matrix1 (exported) = " << matrix1 << std::endl;
std::cout << std::endl << "Matrix1 (imported) = " << matrix1_input << std::endl;
std::cout << std::endl << "Matrix2 (exported) = " << matrix2 << std::endl;
std::cout << std::endl << "Matrix2 (imported) = " << matrix2_input << std::endl;

Complete Source Code

Author
Dirk Klindworth 2014
#include "matfile.hh"
#include "operator.hh"
int main(int argc, char** argv) {
try {
// ** preparations **
for(uint i=0; i<matrix1.dimX(); ++i){
for(uint j=0; j<matrix1.dimY(); ++j){
matrix1(i,j) = std::complex<double>(std::rand(),std::rand());
}
}
for(uint i=0;i<std::min(matrix2.dimX(),matrix2.dimY());++i)
matrix2(i,i) = std::rand();
// ** write matrices to matfile **
concepts::MatfileIO mo("MatfileIO");
mo.add(matrix1,"Matrix1");
mo.add(matrix2,"Matrix2");
mo.close();
// ** read matfile **
concepts::MatfileIO mi("MatfileIO");
std::cout << "exists(Matrix1): " << mi.exists("Matrix1") << std::endl;
std::cout << "isScalar(Matrix1): " << mi.isScalar("Matrix1") << std::endl;
std::cout << "isDense(Matrix1): " << mi.isDense("Matrix1") << std::endl;
std::cout << "isSparse(Matrix1): " << mi.isSparse("Matrix1") << std::endl;
std::cout << "isUint(Matrix1): " << mi.isUint("Matrix1") << std::endl;
std::cout << "isInt(Matrix1): " << mi.isInt("Matrix1") << std::endl;
std::cout << "isReal(Matrix1): " << mi.isReal("Matrix1") << std::endl;
std::cout << "isCmplx(Matrix1): " << mi.isCmplx("Matrix1") << std::endl;
// ** load matrices from matfile **
if ((mi.exists("Matrix1")) && (mi.isDense("Matrix1")) && (mi.isCmplx("Matrix1")))
mi.get(matrix1_input,"Matrix1");
if ((mi.exists("Matrix2")) && (mi.isSparse("Matrix2")) && (mi.isReal("Matrix2")))
mi.get(matrix2_input,"Matrix2");
mi.close();
// ** output of program **
std::cout << std::endl << "Matrix1 (exported) = " << matrix1 << std::endl;
std::cout << std::endl << "Matrix1 (imported) = " << matrix1_input << std::endl;
std::cout << std::endl << "Matrix2 (exported) = " << matrix2 << std::endl;
std::cout << std::endl << "Matrix2 (imported) = " << matrix2_input << std::endl;
}
std::cout << e << std::endl;
return 1;
}
}
bool exists(const std::string &varName) const
Returns true if the variable /c varName exists, false otherwise.
bool isCmplx(const std::string &varName) const
Return true if the variable /c varName is of type double complex.
Base class for exceptions.
Definition: exceptions.hh:86
void get(T &u, const std::string varName)
Method to get various objects out of the current open MatfileIO.
Definition: matfileIO.hh:233
Concepts *.mat-file tool.
Definition: matfileIO.hh:112
bool isScalar(const std::string &varName) const
Return true if the variable /c varName is a scalar.
bool isReal(const std::string &varName) const
Return true if the variable /c varName is of type double.
bool isUint(const std::string &varName) const
Returns true if the variable /c varname is uint, else 0.
void close()
Method to close the current *.mat-file.
F min(const concepts::Array< F > &a)
Returns the minimal value in array a.
Definition: arrayOp.hh:67
std::complex< Real > Cmplx
Type for a complex number. It also depends on the setting of Real.
Definition: typedefs.hh:39
bool isSparse(const std::string &varName) const
Returns true if the variable /c varName is sparse, else 0.
bool isInt(const std::string &varName) const
Returns true if the variable /c varname is int, else 0.
bool isDense(const std::string &varName) const
Returns true if the variable /c varName is dense, else 0.
void add(const T &u, const std::string varName, enum matio_compression compress=MAT_COMPRESSION_NONE)
Method to add various objects to a current open MatfileIO.
Definition: matfileIO.hh:160
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich