varmats_from_mat#
- scipy.io.matlab.varmats_from_mat(file_obj)[source]#
Pull variables out of mat 5 file as a sequence of mat file objects
This can be useful with a difficult mat file, containing unreadable variables. This routine pulls the variables out in raw form and puts them, unread, back into a file stream for saving or reading. Another use is the pathological case where there is more than one variable of the same name in the file; this routine returns the duplicates, whereas the standard reader will overwrite duplicates in the returned dictionary.
The file pointer in file_obj will be undefined. File pointers for the returned file-like objects are set at 0.
- Parameters:
- file_objfile-like
file object containing mat file
- Returns:
- named_matslist
list contains tuples of (name, BytesIO) where BytesIO is a file-like object containing mat file contents as for a single variable. The BytesIO contains a string with the original header and a single var. If
var_file_obj
is an individual BytesIO instance, then save as a mat file with something likeopen('test.mat', 'wb').write(var_file_obj.read())
Examples
>>> import scipy.io >>> import numpy as np >>> from io import BytesIO >>> from scipy.io.matlab._mio5 import varmats_from_mat >>> mat_fileobj = BytesIO() >>> scipy.io.savemat(mat_fileobj, {'b': np.arange(10), 'a': 'a string'}) >>> varmats = varmats_from_mat(mat_fileobj) >>> sorted([name for name, str_obj in varmats]) ['a', 'b']