[docs]defloadmat(filename):r"""This function should be called instead of direct spio.loadmat as it cures the problem of not properly recovering python dictionaries from mat files. It calls the function check keys to cure all entries which are still mat-objects. """def_check_keys(d):r"""Checks if entries in dictionary are mat-objects. If yes todict is called to change them to nested dictionaries. """forkeyind:ifisinstance(d[key],spio.matlab.mio5_params.mat_struct):d[key]=_todict(d[key])returnddef_todict(matobj):r"""A recursive function which constructs from matobjects nested dictionaries."""d={}forstrginmatobj._fieldnames:elem=matobj.__dict__[strg]ifisinstance(elem,spio.matlab.mio5_params.mat_struct):d[strg]=_todict(elem)elifisinstance(elem,np.ndarray):d[strg]=_tolist(elem)else:d[strg]=elemreturnddef_tolist(ndarray):r"""A recursive function which constructs lists from cellarrays (which are loaded as numpy ndarrays), recursing into the elements if they contain matobjects. """elem_list=[]forsub_eleminndarray:ifisinstance(sub_elem,spio.matlab.mio5_params.mat_struct):elem_list.append(_todict(sub_elem))elifisinstance(sub_elem,np.ndarray):elem_list.append(_tolist(sub_elem))else:elem_list.append(sub_elem)returnelem_listdata=scipy.io.loadmat(filename,struct_as_record=False,squeeze_me=True)return_check_keys(data)