structs in .mat files are now converted to a dict subclass
@@ -21,8 +21,8 @@ def rtb_load_matfile(filename):
- Uses SciPy ``io.loadmat`` to do the work.
- If the filename has no path component, eg. ``map1.mat`, it will be
first be looked for in the folder ``roboticstoolbox/data``.
- - MATLAB structs are converted to Python named tuples, so the elements
- can be accessed using dot notation.
+ - MATLAB structs are converted to Python dicts, but extended so that
+ elements can be accessed using dot notation.
:seealso: :func:`path_to_datafile`
"""
@@ -37,9 +37,19 @@ def rtb_load_matfile(filename):
# were a MATLAB struct, convert them to a namedtuple
for key, value in data.items():
if isinstance(value, mat_struct):
- print('fixing')
- nt = namedtuple("matstruct", value._fieldnames)
- data[key] = nt(*[getattr(value, n) for n in value._fieldnames])
+
+ # extend dict with a dot access method
+ class dictx(dict):
+ def __getattr__(self, key):
+ try:
+ return self[key]
+ except KeyError as k:
+ raise AttributeError(k)
+
+ d = dictx()
+ for v in value._fieldnames:
+ d[v] = getattr(value, v)
+ data[key] = d
return data
