I’m coding a function in C++ to load a MAT-File (level 5) format based on the MATLAB®
MAT-File Format 2011b doc (see http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf).
I must be missing something (maybe with C++) because the number of bytes field is zero. The MAT-File header is read successfully as is the data type element flag but the number of bytes is not. The piece of code to load the mat file is as follows:
// file handler
ifstream file;
// open file
file.open(i_file, ifstream::in | ifstream::binary);
// check for errors
if (!file) return NULL;
/********** BEGIN MAT-File Header **********/
char header_text[116], header_offset[8], header_version[2], header_endian[2];
// The first 116 bytes of the header can contain text data in human-readable form.
file.read( (char*) &header_text, 116); cout << header_text << endl;
/* Header Subsystem Data Offset Field */
// Bytes 117 through 124 of the header contain an offset to subsystem-specific
// data in the MAT-file.
file.read( (char*) &header_offset, 8); cout << header_offset << endl;
/* Header Flag Fields */
// Version When creating a MAT-file, set this field to 0x0100.
file.read( (char*) &header_version, 2); cout << header_version << endl;
// Endian Indicator. Contains the two characters, M and I, written to the
// MAT-file in this order, as a 16-bit value.
file.read( (char*) &header_endian, 2); cout << header_endian << endl;
/********** END MAT-File Header **********/
/********** BEGIN MAT-File Data Element **********/
/* The Tag Field */
// The 8-byte data element tag is composed of two, 32-bit fields
// Data Type
__int32_t data_type = file.get(); cout << data_type << endl;
// Number of Bytes
__int32_t num_bytes = file.get(); cout << num_bytes << endl;
The output is as follows:
MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri May 20 18:21:46 2011
IM
15
0
From MATLAB I get the information:
whos -file PaviaU.mat
Name Size Bytes Class Attributes
paviaU 610x340x103 170897600 double
Did I incorrectly load the data from the header somehow? Why is Number of bytes equal to zero?
Edit: If I read the data elements as follows (sa suggested in one comment):
char data_type[4], num_bytes[4];
file.read((char*) &data_type, 4); cout << data_type << endl;
file.read((char*) &num_bytes, 4); cout << num_bytes << endl;
I get unexpected values on the cout (binary codes)
But debugging the function I can inspect both variables:
data_type[0] = 15
data_type[1] = 0 '\0'
data_type[2] = 0 '\0'
data_type[3] = 0 '\0'
num_bytes[0] = -3/253
num_bytes[1] = 27
num_bytes[2] = 19
num_bytes[3] = 2
The data_type value is 15 but what about the -3/253 in num_bytes? Which number is that?
According to the spec, your results shown the data type is
The size field shows your data size is 0x02131BFD or 34,806,781 Bytes. Compared to your original size of 170MB, this compression ratio seems reasonable depending on your data.
Assuming you can spare the 140MB, it would probably be easier to save the file as uncompressed data. Saving as an old .mat version disabled compression (mathworks). I don’t know of a way to disable it in new .mat files.
Edit
The size and data type fields can be better read as:
This can be done directly as your machine is little endian. Has the result of the endian field been the opposite, you would have to swap the order of all the bytes before storing them in the uint32.