title: "TypeError: buffer is too small for requested array" when attempting to read a .mat file using scipy.io.loadmat
body: <p>I have such a code:</p>
  
  <pre><code>import tempfile
  import subprocess
  import shlex
  import os
  import numpy as np
  import scipy.io
  
  script_dirname = os.path.abspath(os.path.dirname(__file__))
  
  
  def get_windows(image_fnames, cmd='selective_search'):
      f, output_filename = tempfile.mkstemp(suffix='.mat')
      os.close(f)
      fnames_cell = '{' + ','.join("'{}'".format(x) for x in image_fnames) + '}'
      command = "{}({}, '{}')".format(cmd, fnames_cell, output_filename)
      print(command)
  
      mc = "matlab -nojvm -r \"try; {}; catch; exit; end; exit\"".format(command)
      pid = subprocess.Popen(
          shlex.split(mc), stdout=open('/dev/null', 'w'), cwd=script_dirname)
      retcode = pid.wait()
      if retcode != 0:
          raise Exception("Matlab script did not exit successfully!")
  
      all_boxes = list(scipy.io.loadmat(output_filename)['all_boxes'][0])
      subtractor = np.array((1, 1, 0, 0))[np.newaxis, :]
      all_boxes = [boxes - subtractor for boxes in all_boxes]
  
      os.remove(output_filename)
      if len(all_boxes) != len(image_fnames):
          raise Exception("Something went wrong computing the windows!")
      return all_boxes
  
  if __name__ == '__main__':
  
      import time
  
      image_filenames = [
          script_dirname + '/000015.jpg',
          script_dirname + '/cat.jpg'
      ] * 4
      t = time.time()
      boxes = get_windows(image_filenames)
      print(boxes[:2])
      print("Processed {} images in {:.3f} s".format(
          len(image_filenames), time.time() - t))
  </code></pre>
  
  <p>The code is tested, and it has to work. </p>
  
  <p>When I run the code, I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "selective_search.py", line 62, in &lt;module&gt;
      boxes = get_windows(image_filenames)
  
    File "selective_search.py", line 42, in get_windows
      all_boxes = list(scipy.io.loadmat(output_filename)['all_boxes'][0])
  
    File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 131, in loadmat
      MR = mat_reader_factory(file_name, appendmat, **kwargs)
  
    File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 55, in mat_reader_factory
      mjv, mnv = get_matfile_version(byte_stream)
  
    File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/miobase.py", line 218, in get_matfile_version
      buffer = fileobj.read(4))
  
  TypeError: buffer is too small for requested array
  </code></pre>
  
  <p>I'm using MATLAB 2015. </p>
  
  <p>How can I fix the problem?</p>