meanFieldInCells

PURPOSE ^

MEANFIELDINCELLS compute mean field value of structures in cell arrays.

SYNOPSIS ^

function [ meanValue ] = meanFieldInCells( cellArray, fieldName )

DESCRIPTION ^

 MEANFIELDINCELLS compute mean field value of structures in cell arrays.

   [ meanValue ] = meanFieldInCells( cellArray, fieldName )
   meanFieldInCells() compute mean value of specific field in structure cell
   array. If only one input argument exist, this function will compute all
   numeric type fields in cellArray.

   Input:
       cellArray: structures in cell array
       fieldName: name of field you want to compute

   Output:
       meanValue: mean value of specific field in cellArray. If you compute all
           numeric field, meanValue will be a structure containing all mean of
           numeric field in cellArray.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ meanValue ] = meanFieldInCells( cellArray, fieldName )
0002 % MEANFIELDINCELLS compute mean field value of structures in cell arrays.
0003 %
0004 %   [ meanValue ] = meanFieldInCells( cellArray, fieldName )
0005 %   meanFieldInCells() compute mean value of specific field in structure cell
0006 %   array. If only one input argument exist, this function will compute all
0007 %   numeric type fields in cellArray.
0008 %
0009 %   Input:
0010 %       cellArray: structures in cell array
0011 %       fieldName: name of field you want to compute
0012 %
0013 %   Output:
0014 %       meanValue: mean value of specific field in cellArray. If you compute all
0015 %           numeric field, meanValue will be a structure containing all mean of
0016 %           numeric field in cellArray.
0017 
0018 %   ---------
0019 %   Yen-Nan Lin, NTHU, 2010-2014, Matlab 2012a
0020 
0021 isAllField = false;
0022 if nargin == 1
0023     isAllField = true;
0024 end
0025 nCell = length(cellArray);
0026 if nCell == 0
0027     error('Input cell array is empty.');
0028 end
0029 if isAllField == true
0030     meanValue = [];
0031     allFieldName = fieldnames(cellArray{1});
0032     nField = length(allFieldName);
0033     for iField = 1:nField
0034         iFieldName = allFieldName{iField};
0035         iFieldValue = getfield(cellArray{1}, iFieldName);
0036         if ~isnumeric(iFieldValue)
0037             continue;
0038         end
0039         meanFieldValue = meanSpecificField(cellArray, iFieldName);
0040         meanValue = setfield(meanValue, iFieldName, meanFieldValue);
0041     end
0042 else
0043     meanValue = meanSpecificField(cellArray, fieldName);
0044 end
0045 end
0046 
0047 function [ meanValue ] = meanSpecificField( cellArray, fieldName )
0048 nCell = length(cellArray);
0049 sumField = getfield(cellArray{1}, fieldName);
0050 if ~isnumeric(sumField)
0051     error([fieldName, ' is not numeric.']);
0052 end
0053 for iCell = 2:nCell
0054     if ~isfield(cellArray{iCell}, fieldName)
0055         error(['No ' fieldName 'field in ' int2str(iCell) 'th structure.']);
0056     end
0057     sumField = getfield(cellArray{iCell}, fieldName) + sumField;
0058 end
0059 meanValue = sumField / nCell;
0060 end

Generated on Thu 30-Jan-2014 00:00:07 by m2html © 2005