def _todict(matobj):
    """
    a recursive function which constructs from matobjects nested dictionaries
    """
    dictionary = {}
    #noinspection PyProtectedMember
    for strg in matobj._fieldnames:
        elem = matobj.__dict__[strg]
        if isinstance(elem, matlab.mio5_params.mat_struct):
            dictionary[strg] = _todict(elem)
        else:
            dictionary[strg] = elem
    return dictionary