MATLAB: How to get max, min and avg of a field of a structure in a cell matrix

cellmatrixstructures

Hi
I have a cell matrix consisting of 3 columns and 400 rows. Every entry in this matrix is a structure. Lets say three columns are A, B and C. Column A contains same structure StructureA, Column B contains same structure StructureB, Column C contains same structure StructureC in all rows. Let say StructureC contains 4 fields field1, field2, field3 and field4.
  1. I want to find max and min value of field3 across all rows in column C.
  2. Then I want to find out which row of ColumnC contains max value and which row contains min value of field3.
  3. Then I want to find out the average of field3 across all rows in ColumnC.
  4. Finally I want to get all the rows where field3 equals to some value, such as get all the rows where field3 = 'abc' in ColumnC.
I have already tried below code to calculate the max value but it did not work
maxField3 = max(Column3{:,3}.field3)
I don't want to use loop for calculating these things. Please help.
Thanks in advance.

Best Answer

I would suggest that you convert your data in a non-scalar structure, then your task become trivial using basic indexing and commands. The best solution would be to get rid of the cell array entirely, and have just one non-scalar structure. If the three columns require different fields, then three non-scalar structures will be required.
Hers is an example of a non-scalar structure, and how easy it is access its data:
>> S = struct('f1',{1,2,3,4},'f2',{10,20,30,0});
>> size(S)
ans = 1 4
>> S(3).f1
ans = 3
>> [S.f1]
ans = 1 2 3 4
>> [val,idx] = max([S.f2])
val = 30
idx = 3
You can also use indexing, arrayfun, and many other operations.
You will find your code a lot simpler to write if you use non-scalar structure instead of putting lots of separate structures into a cell array. Here is how to concatenate a cell array of structures into one non-scalar structure:
>> X = {struct('f1',1,'f2',10);struct('f1',2,'f2',20);struct('f1',3,'f2',30)};
>> S = vertcat(X{:});