This tutorial shows how to export and import data from Concepts to files in Matlab's binary format.
-
Commented Program
-
Preparations
-
Export data from Concepts to a matfile
-
Import data to Concepts from a matfile
-
Complete Source Code
Commented Program
Include files for input/output handling and matrices.
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.
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.
Import data to Concepts from a matfile
Now we want to (re-)import the matrices from the matfile. We open the matfile
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.
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 "operator.hh"
int main(int argc, char** argv) {
try {
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();
mo.
add(matrix1,
"Matrix1");
mo.
add(matrix2,
"Matrix2");
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;
mi.
get(matrix1_input,
"Matrix1");
mi.
get(matrix2_input,
"Matrix2");
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;
}
}