So I finally made the output to satisfy my needs. The result is very similar to what iavr wrote (thank you for your quick response!), however I will copy my full (working) codes so it can be beneficial for someone else too.
This is mine function of class file_io
that is writing the data:
void file_io::writeColDataBin(vector< vector<double> >* data){
double rows = (double)(*data).size();
double cols = (double)(*data)[0].size();
file.write(reinterpret_cast<const char*>(&rows), sizeof(rows));
file.write(reinterpret_cast<const char*>(&cols), sizeof(cols));
for(unsigned int j = 0;j < (*data)[0].size();j++){
for(unsigned int i = 0;i < (*data).size();i++){
file.write(reinterpret_cast<const char*>(&(*data)[i][j]), sizeof((*data)[i][j]));
}
}
}
It simply writes the number of rows and columns first, then continues as normal, however writing the cells of matrix by rows first in every column, just then moving to next column. This is important, because Matlab is ordering its fread by columns, not rows. Size of rows and columns is also transformed into double, so Matlab can read the whole file at once.
The same class also opens a file of ofstream file
as:
void file_io::fileOpenBin(const char* fileName){
file.open(fileName, ios::out | ios::binary | ios::trunc);
}
After this, matrix is exported and loaded into Matlab with:
fid = fopen('data.dat','r');
data = fread(fid,'double');
fclose(fid);
rows = data(1);
cols = data(2);
data(1:2) = [];
data = reshape(data,rows,cols);
Rows and columns are imported, then first two cells are deleted from data and then they are reshaped into matrix needed.
Hope this helps someone in the future, it is probably not the quickest binary reading process, but it is definitely many times faster than reading ASCII.