stdFieldInCells

PURPOSE ^

MEANFIELDINCELLS compute std field value of structures in cell arrays.

SYNOPSIS ^

function [ stdCell ] = stdFieldInCells( cellArray, fieldName )

DESCRIPTION ^

 MEANFIELDINCELLS compute std field value of structures in cell arrays.

   [ stdCell ] = stdFieldInCells( cellArray, fieldName )
   stdFieldInCells() compute std 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:
       stdCell: std value of specific field in cellArray. If you compute all
           numeric field, stdCell will be a structure containing all std of
           numeric field in cellArray.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ stdCell ] = stdFieldInCells( cellArray, fieldName )
0002 % MEANFIELDINCELLS compute std field value of structures in cell arrays.
0003 %
0004 %   [ stdCell ] = stdFieldInCells( cellArray, fieldName )
0005 %   stdFieldInCells() compute std 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 %       stdCell: std value of specific field in cellArray. If you compute all
0015 %           numeric field, stdCell will be a structure containing all std 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 
0030 if isAllField == true
0031     stdCell = [];
0032     allFieldName = fieldnames(cellArray{1});
0033     nField = length(allFieldName);
0034     % compute std value of each field
0035     for iField = 1:nField
0036         iFieldName = allFieldName{iField};
0037         iFieldValue = getfield(cellArray{1}, iFieldName);
0038         if ~isnumeric(iFieldValue)
0039             continue;
0040         end
0041         stdFieldValue = stdSpecificField(cellArray, iFieldName);
0042         stdCell = setfield(stdCell, iFieldName, stdFieldValue);
0043     end
0044 else
0045     stdCell = stdSpecificField(cellArray, fieldName);
0046 end
0047 end
0048 
0049 function [ stdFieldValue ] = stdSpecificField( cellArray, fieldName )
0050 % compute std value of specific field in cell array
0051 
0052 nCell = length(cellArray);
0053 fieldValue = full(getfield(cellArray{1}, fieldName));
0054 if ~isnumeric(fieldValue)
0055     error([fieldName, ' is not numeric.']);
0056 end
0057 catDimIx = find(size(fieldValue) == 1, 1);
0058 if isempty(catDimIx)
0059     catDimIx = numel(size(fieldValue)) + 1;
0060 end
0061 
0062 % Concatenate all value of specific field in cell array
0063 for iCell = 2:nCell
0064     if ~isfield(cellArray{iCell}, fieldName)
0065         error(['No ' fieldName 'field in ' int2str(iCell) 'th structure.']);
0066     end
0067     fieldValue = ...
0068         cat(catDimIx, fieldValue, full(getfield(cellArray{iCell}, fieldName)));
0069 end
0070 
0071 stdFieldValue = std(fieldValue, 0, catDimIx);
0072 end

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