This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Read and write large Matrix using binary file

Tags: None
(comma "," separated)
Bazinga84
Registered Member
Posts
1
Karma
0
Hello everyone,

I'm trying to read and write a large float Eigen Matrix (size is approx 8K * 8K). Currently, my code looks like this:

Code: Select all

//choose filename
QString filename = ("filename.dat");

//write on binary file
std::ofstream outfile( filename.toAscii(), std::ios::binary );
outfile.write( (char *) my_matrix, sizeof( *my_matrix) );
outfile.close();


//reading from file
std::ifstream infile( filename.toAscii(), std::ios::binary );

//allocate space
my_matrix2 = new Eigen::MatrixXf( 8024 ,8024 );

infile.read( (char *) my_matrix2 , sizeof( *my_matrix2 ) );
infile.close();


The matrix my_matrix is dynamically allocated and, before writing, is correctly initialized (I made some tests). However, this code has the effect of creating a small file (1Kb) in my directory, which turns out to be unuseful, since after the read instruction
all the value in the new matrix are equal to zero. Is it possible to store
the matrix in some binary file? Or do I need to use only text file?
Thanks for your advice!
jitseniesen
Registered Member
Posts
204
Karma
2
An object of type MatrixXf does not contain the values, but it contains a pointer to a dynamically allocated region of memory containing the values. This is the pointer returned by .data() .

I did not try it, but I think you can write and read the values with
Code: Select all
// write values
outfile.write( (char *) my_matrix.data(), my_matrix.rows() * my_matrix.cols() * sizeof(float) );
// ...
// declare my_matrix2 of correct size
// ...
// read values
infile.read( (char *) my_matrix2.data(), my_matrix2.rows() * my_matrix2.cols() * sizeof(float) );

This does not store the size of the matrix in the file.
TC1
Registered Member
Posts
4
Karma
0
OS
Should work, but serialization is really one of those things you should leave to a readily available & widely tested library. I personally prefer Boost, but any other should work. If you get the need to gain access to any inside data or add methods to Eigen (which might be the case with Boost), you can use the plugin system.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]