How to sort a 1x1 struct based on the struct in the deep level of the struct?

1 view (last 30 days)
A is a 1x1 struct. A.EachFrame is also a 1x1 struct. There are 32 1x1 struct inside A.EachFrame:
Item_1
Item_3
Item_5
...
Item_63
Inside each item, it is Position.Item_1.RealPosition(1). RealPosition(1) is a number. Item_1 here is fixed. For outside Item_1, Item_3 to Item_63, RealPosition(1) are different:
73.5
70.5000000000000
67.5000000000000
64.5000000000000
61.5000000000000
58.5000000000000
55.5000000000000
52.5000000000000
49.5000000000000
46.5000000000000
43.5000000000000
40.5000000000000
37.5000000000000
34.5000000000000
31.5000000000000
28.5000000000000
121.500000000000
118.500000000000
115.500000000000
112.500000000000
109.500000000000
106.500000000000
103.500000000000
100.500000000000
97.5000000000000
94.5000000000000
91.5000000000000
88.5000000000000
85.5000000000000
82.5000000000000
79.5000000000000
76.5000000000000
I want to sort Outside Item_1, Item_3 to Item_63, according to RealPosition(1) value, from low to high (or high to low). The expected result should be the first item is the one with the lowest RealPosition(1) value, in this case, Item_31, with RealPosition(1)=28.5.
I exacted all RealPosition(1) to an 1D array pos_array. I tried to use
snew = orderfields(A.EachFrame, round(pos_array));
It didn't work because A.EachFrame was 1x1 struct but there were 12 elements in pos_array. So how to re-arrange this 1x1 struct based on the value in the deep level of the struct? Many thanks.

Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!