Search code examples
pythonmatlabnumpydefaultdict

Implementing Matlab structure in python in a flexible way


I want to implement a structure as flexible as Matlab struct in python. I have looked at Matlab structure in Python [duplicate] but the difference is I want a flexible structure where the field names and the depth of the structure are not know in advance. So for example in Matlab you can say:

parent.child1.grandchild1=2
parent.child1.grandchild2=3
parent.child2.grandchild1.grand-grandchild1=1

without initializing thet struct with the field names. I want to implement a structure that can be called whenever a user (who is not used to python) wants to store the value of a measurement in the structure he is using. So the call can happen multiple times in a code. I have looked at nested defaultdicts but did not really find a solution. I have read that numpy array are normally used for this purpose, but how can I constantly add fields to a numpy array without loosing performance? Any help would be greatly appreciated.


Solution

  • Originally MATLAB only had 2d matrices. Those are similar to numpy.ndarray. Actually numpy.matrix is a subclass that is always 2d, meant for wayward MATLAB programmers.

    MATLAB then added cell, which are somewhat like python list. That is they can hold references to any kind of object.

    Then it added struct. That's somewhat like the python classes. A class normally should have all attributes defined at the start, but it is often possible to add an attribute simply by assignment

    anObject.foobar = "astring"
    

    It went on to add a class system, first as a crude add on to struct, and then a bit more refined. But in contrast, in python everything is an object, an instance of some class.

    scipy.io.loadmat can read MATLAB .mat files (up to something like version 7.3). It returns a numpy array - with object dtype for cell, and structured arrays for struct. But none of those have the kind of extensibility that you see in MATLAB.