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
First we have to import concepts module from the libconceptspy
package.
Preparations
In the first lines two matrices are initialized.
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 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.
Then we use the method add
to add data to the created matfile.
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
.
We read the two matrices from the matfile making sure that they exist and have the correct properties.
We close the matfile.
Finally, we print the exported and imported matrices on the screen.
Complete Source Code
1from libconceptspy import concepts
2import random
3
4def 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
18 compress = concepts.MatfileIO.matio_compression(0)
19 mo.add(matrix1,"Matrix1",compress)
20 mo.add(matrix2,"Matrix2",compress)
21 mo.close()
22
23
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
50if __name__ == '__main__':
51 main()