title: import Level 5 MAT-File Format data from file in C++
body: <p>I'm coding a function in C++ to load a MAT-File (level 5) format based on the <em>MATLAB®
  MAT-File Format 2011b</em> doc (see www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf).</p>
  
  <p>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:</p>
  
  <pre><code>// 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*) &amp;header_text, 116); cout &lt;&lt; header_text &lt;&lt; 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*) &amp;header_offset, 8); cout &lt;&lt; header_offset &lt;&lt; endl;
  /* Header Flag Fields */
  // Version When creating a MAT-file, set this field to 0x0100.
  file.read( (char*) &amp;header_version, 2); cout &lt;&lt; header_version &lt;&lt; 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*) &amp;header_endian, 2); cout &lt;&lt; header_endian &lt;&lt; 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 &lt;&lt; data_type &lt;&lt; endl;
  // Number of Bytes
  __int32_t num_bytes = file.get(); cout &lt;&lt; num_bytes &lt;&lt; endl;
  </code></pre>
  
  <p>The output is as follows:</p>
  
  <pre><code>MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri May 20 18:21:46 2011
  
  
  IM
  15
  0
  </code></pre>
  
  <p>From MATLAB I get the information:</p>
  
  <blockquote>
    <p>whos -file PaviaU.mat</p>
    
    <p>Name          Size                     Bytes  Class     Attributes</p>
    
    <p>paviaU      610x340x103            170897600  double   </p>
  </blockquote>
  
  <p>Did I incorrectly load the data from the header somehow? <strong>Why is Number of bytes equal to zero?</strong></p>
  
  <p><strong>Edit</strong>: If I read the data elements as follows (sa suggested in one comment):</p>
  
  <pre><code>char data_type[4], num_bytes[4];
  file.read((char*) &amp;data_type, 4); cout &lt;&lt; data_type &lt;&lt; endl;
  file.read((char*) &amp;num_bytes, 4); cout &lt;&lt; num_bytes &lt;&lt; endl;
  </code></pre>
  
  <p>I get unexpected values on the <code>cout</code> (binary codes)</p>
  
  <p>But debugging the function I can inspect both variables:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The data_type value is 15 but what about the -3/253 in <code>num_bytes</code>? Which number is that?</p>