matfileTutorial.py

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

First we have to import concepts module from the libconceptspy package.

1 from libconceptspy import concepts


Preparations

In the first lines two matrices are initialized.

7  matrix1 = concepts.DenseMatrix_c(m=5,n=3)
8  matrix2 = concepts.SparseMatrix_r(m=4)
9  for i in range(matrix1.dimX()):
10  for j in range(matrix1.dimY()):
11  value = random.random() + 1j * random.random()
12  matrix1.setEntry(i,j,value)
13 
14  for i in range(min(matrix2.dimX(),matrix2.dimY())):
15  matrix2.setEntry(i,i,random.random())

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.

19  mo = concepts.MatfileIO("MatfileIO")

Then we define MAT file compression options which could either be no compression indicated by '0' or zlib compression indicated by '1'. Here, we choose no compression option.

21  compress = concepts.MatfileIO.matio_compression(0)

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

23  mo.add(matrix1,"Matrix1",compress)
24  mo.add(matrix2,"Matrix2",compress)

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

26  mo.close()

Import data to Concepts from a matfile

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

30  mi = concepts.MatfileIO("MatfileIO")

and check the properties of the variable Matrix1.

32  print "exists(Matrix1): ",mi.exists("Matrix1")
33  print "isScalar(Matrix1): ",mi.isScalar("Matrix1")
34  print "isDense(Matrix1): ",mi.isDense("Matrix1")
35  print "isSparse(Matrix1): ",mi.isSparse("Matrix1")
36  print "isUint(Matrix1): ",mi.isUint("Matrix1")
37  print "isInt(Matrix1): ",mi.isInt("Matrix1")
38  print "isReal(Matrix1): ",mi.isReal("Matrix1")
39  print "isCmplx(Matrix1): ",mi.isCmplx("Matrix1")

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

43  matrix1_input = concepts.DenseMatrix_c()
44  if(mi.exists('Matrix1') and mi.isDense('Matrix1') and mi.isCmplx("Matrix1")):
45  mi.get(matrix1_input,"Matrix1")
46 
47  matrix2_input = concepts.SparseMatrix_r()
48  if(mi.exists('Matrix2') and mi.isSparse('Matrix2') and mi.isReal("Matrix2")):
49  mi.get(matrix2_input,"Matrix2")

We close the matfile.

51  mi.close()

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

55  print 'Matrix1 (exported) = ', matrix1
56  print 'Matrix1 (imported) = ', matrix1_input
57  print 'Matrix2 (exported) = ', matrix2
58  print 'Matrix2 (imported) = ', matrix2_input

Complete Source Code

1 from libconceptspy import concepts
2 import random
3 
4 def main():
5 
6  matrix1 = concepts.DenseMatrix_c(m=5,n=3)
7  matrix2 = concepts.SparseMatrix_r(m=4)
8  for i in range(matrix1.dimX()):
9  for j in range(matrix1.dimY()):
10  value = random.random() + 1j * random.random()
11  matrix1.setEntry(i,j,value)
12 
13  for i in range(min(matrix2.dimX(),matrix2.dimY())):
14  matrix2.setEntry(i,i,random.random())
15 
16 
17  mo = concepts.MatfileIO("MatfileIO")
18  compress = concepts.MatfileIO.matio_compression(0)
19  mo.add(matrix1,"Matrix1",compress)
20  mo.add(matrix2,"Matrix2",compress)
21  mo.close()
22 
23 
24  mi = concepts.MatfileIO("MatfileIO")
25  print "exists(Matrix1): ",mi.exists("Matrix1")
26  print "isScalar(Matrix1): ",mi.isScalar("Matrix1")
27  print "isDense(Matrix1): ",mi.isDense("Matrix1")
28  print "isSparse(Matrix1): ",mi.isSparse("Matrix1")
29  print "isUint(Matrix1): ",mi.isUint("Matrix1")
30  print "isInt(Matrix1): ",mi.isInt("Matrix1")
31  print "isReal(Matrix1): ",mi.isReal("Matrix1")
32  print "isCmplx(Matrix1): ",mi.isCmplx("Matrix1")
33 
34 
35  matrix1_input = concepts.DenseMatrix_c()
36  if(mi.exists('Matrix1') and mi.isDense('Matrix1') and mi.isCmplx("Matrix1")):
37  mi.get(matrix1_input,"Matrix1")
38 
39  matrix2_input = concepts.SparseMatrix_r()
40  if(mi.exists('Matrix2') and mi.isSparse('Matrix2') and mi.isReal("Matrix2")):
41  mi.get(matrix2_input,"Matrix2")
42  mi.close()
43 
44 
45  print 'Matrix1 (exported) = ', matrix1
46  print 'Matrix1 (imported) = ', matrix1_input
47  print 'Matrix2 (exported) = ', matrix2
48  print 'Matrix2 (imported) = ', matrix2_input
49 
50 if __name__ == '__main__':
51  main()
Concepts *.mat-file tool.
Definition: matfileIO.hh:112
F min(const concepts::Array< F > &a)
Returns the minimal value in array a.
Definition: arrayOp.hh:67
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich