Complex matlab-like data structure in python (numpy/scipy)

I’ve often seen the following conversion approaches:

matlab array -> python numpy array

matlab cell array -> python list

matlab structure -> python dict

So in your case that would correspond to a python list containing dicts, which themselves contain numpy arrays as entries

item[i]['attribute1'][2,j]

Note

Don’t forget the 0-indexing in python!

[Update]

Additional: Use of classes

Further to the simple conversion given above, you could also define a dummy class, e.g.

class structtype():
    pass

This allows the following type of usage:

>> s1 = structtype()
>> print s1.a
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-40-7734865fddd4> in <module>()
----> 1 print s1.a
AttributeError: structtype instance has no attribute 'a'
>> s1.a=10
>> print s1.a
10

Your example in this case becomes, e.g.

>> item = [ structtype() for i in range(10)]
>> item[9].a = numpy.array([1,2,3])
>> item[9].a[1]
2

Leave a Comment