def varmats_from_mat(file_obj):
 rdr = MatFile5Reader(file_obj)
 file_obj.seek(0)
 hdr_len = MDTYPES[native_code]['dtypes']['file_header'].itemsize
 raw_hdr = file_obj.read(hdr_len)
 file_obj.seek(0)
 rdr.initialize_read()
 mdict = rdr.read_file_header()
 next_position = file_obj.tell()
 named_mats = []
 while (not rdr.end_of_stream()):
  start_position = next_position
  (hdr, next_position) = rdr.read_var_header()
  name = asstr(hdr.name)
  file_obj.seek(start_position)
  byte_count = (next_position - start_position)
  var_str = file_obj.read(byte_count)
  out_obj = BytesIO()
  out_obj.write(raw_hdr)
  out_obj.write(var_str)
  out_obj.seek(0)
  named_mats.append((name, out_obj))
 return named_mats

def varmats_from_mat ( file_obj ) : rdr = MatFile5Reader ( file_obj ) file_obj . seek ( 0 ) hdr_len = MDTYPES [ native_code ] [ 'dtypes' ] [ 'file_header' ] . itemsize raw_hdr = file_obj . read ( hdr_len ) file_obj . seek ( 0 ) rdr . initialize_read ( ) mdict = rdr . read_file_header ( ) next_position = file_obj . tell ( ) named_mats = [ ] while ( not rdr . end_of_stream ( ) ) : start_position = next_position ( hdr , next_position ) = rdr . read_var_header ( ) name = asstr ( hdr . name ) file_obj . seek ( start_position ) byte_count = ( next_position - start_position ) var_str = file_obj . read ( byte_count ) out_obj = BytesIO ( ) out_obj . write ( raw_hdr ) out_obj . write ( var_str ) out_obj . seek ( 0 ) named_mats . append ( ( name , out_obj ) ) return named_mats
pull variables out of mat 5 file as a sequence of mat file objects this can be useful with a difficult mat file .