3

I am importing a large struct from Matlab to Python using the scipy.io. The data seems to be imported OK, but it is full of nested lists, which I am not able to simplify. Here's one of the simplest examples:

sc = sio.loadmat(matlabFile)
sc['DieID']['w']

which results in the following output:

array([[array([[ 1309.09090909]]), array([[ 1309.09090909]]),
    array([[1000]], dtype=uint16)]], dtype=object)

Could you please suggest how do I reduce this particular field to a simple list [1309.09090909, 1309.09090909, 1000]?

Thank you for your help!

Chocolate
  • 31
  • 2
  • 2
    Is this what you're looking for? http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python – tiktok Nov 13 '14 at 19:39
  • together with @tiktok answer, you can use `tolist()` method in numpy to get the nested list equivalent of your `array`. – ha9u63a7 Nov 13 '14 at 19:41
  • 1
    Just curious, what does the original matlabFile look like (or at least the relevant parts that you've listed here)? What does it look like when there are more than 3 values? In this case was `w` a 1x3 vector? – cod3monk3y Nov 13 '14 at 19:42
  • Sorry for the delay, and thank you for your replies. In Matlab, the structure is saved in a *.mat file. The structure itself is a fairly large nested struct. For this particular example, here is a subset of the structure: – Chocolate Nov 17 '14 at 15:53
  • sc is a struct, with one of the fields being DieID. DieID is itself a 1x3 struct array, and one of its fields is w. @tiktok, I am not sure if I am doing something wrong, but the solution in the link that you've provided does not seem to reduce my output: I used print(list(itertools.chain.from_iterable(sc['DieID']['w']))), which still resulted in a nested array. – Chocolate Nov 17 '14 at 16:00
  • Did you try print(list(itertools.chain(*sc['DieID']['w']))) as well? – haraprasadj Dec 03 '14 at 14:02
  • Thank you for all the comments. I've used the approach in the link given by @tiktok, but had to go down the nested list multiple times to fully flatten it. – Chocolate Dec 03 '14 at 19:33

0 Answers0