If we consider the following generator function …
def loadrun(runs, varnames=None):
for fle in runs:
yield loadmat(fle, variable_names=varnames)
which we call as follows …
vnames = ['targetpos', 'cuepos', 'soa', 'response']
trials = loadrun(datafiles, vnames) # datafiles is a list of paths to files
we get the following output when listing the length of the resulting dict:
[in] for t in trials:
print len(t)
[out] 7
128
128
128
128
128
128
128
All .mat files contain 124 variables, so only the first output is correct. What gives?
EDIT
I further reduced the scope of the problem. It seems as though loadmat‘s variable_names field fails after the first iteration in any loop. Any idea what’s going on here?
for df in datafiles:
print len(loadmat(df, variable_names=vnames))
[out] 7
128
128
128
128
128
128
128
scipy.io.loadmatis being naughty[1] and mutating the list passed tovariable_names. After the first call toloadmat,varnamesis the empty list.You can workaround this by defining a temporary variable,
_varnames:For example:
Actually, it appears the problem originates in both mio4.py and mio5.py, where
MatFile4ReaderandMatFile5Readercalls theremovemethod onvariable_names: