Recommendation for Complex Object Array Structure

2 views (last 30 days)
I'm struggling with the best way to organize a matlab data structure. Each element has multiple properties associated with it. The multiple properties always exist one per data element and always exist for every data element. It seemed like the best organization was an array of objects, where the object was a class containing the various properties for a single data element. The problem is that much of the processing that needs to be done is across an array of a single property from all the elements. This makes the MATLAB organization difficult, where by default objArray(1:N).prop1 does not give me an array of prop1. I realize I can just take [objArray(1:N).prop1] to turn it into an array, but that isn't very memory efficient I know. Also, I would like to be able to easily set all the properties from a vector (i.e. objArray(1:N).prop1 = newData(1:N) ) and I can't figure out any easy way to make that work. I looked into overloading the set function, but it never seems to work. Having a single object with arrays of each property seems backwards to me, although maybe it would work better? Probably requires a ton of error checking though, to make sure each property remains the same size when you add data to one property or another. Any suggestions? What's the best way to keep the properties together for each data element, but still be able to treat individual properties across multiple elements as an array?

Answers (1)

Aurele Turnes
Aurele Turnes on 5 Aug 2014
It seems like a table might be a good option in your case. Refer to the following documentation page to see the advantages of using tables and learn about basic operations:
Using the example code in the documentation page for instance:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,...
'RowNames',LastName)
You could access the 'weight' for each patient in an array by simply doing:
all_weigth = T{:,'Weight'}
If your data is currently stored in a structure, you can use the struct2table function (see the documentation )

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!