Home > lattice > atgetcells.m

atgetcells

PURPOSE ^

ATGETCELLS performs a search on MATLAB cell arrays of structures

SYNOPSIS ^

function ok=atgetcells(cellarray, field, varargin)

DESCRIPTION ^

ATGETCELLS performs a search on MATLAB cell arrays of structures

 OK = ATGETCELLS(RING, 'field')
   returns indexes of elements that have a field named 'field'

 OK = ATGETCELLS(RING, 'field', VALUE1...)
   returns indexes of elements whose field 'field'
   is equal to VALUE1, VALUE2, ... or VALUEN. Where VALUE can either be
   character strings or a number. If its a character string REGULAR
   expressions can be used.

 OK = ATGETCELLS(RING, 'field', @TESTFUNCTION, ARGS...)
   Uses the user-defined TESTFUNCTION to select array elements
   TESTFUNCTION must be of the form:
       OK=TESTFUNTION(ATELEM,FIELDVALUE,ARGS...)

 OK is a logical array with the same size as RING, refering to matching
 elements in RING

 See also ATGETFIELDVALUES, ATSETFIELDVALUES, FINDCELLS, REGEXPI

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ok=atgetcells(cellarray, field, varargin)
0002 %ATGETCELLS performs a search on MATLAB cell arrays of structures
0003 %
0004 % OK = ATGETCELLS(RING, 'field')
0005 %   returns indexes of elements that have a field named 'field'
0006 %
0007 % OK = ATGETCELLS(RING, 'field', VALUE1...)
0008 %   returns indexes of elements whose field 'field'
0009 %   is equal to VALUE1, VALUE2, ... or VALUEN. Where VALUE can either be
0010 %   character strings or a number. If its a character string REGULAR
0011 %   expressions can be used.
0012 %
0013 % OK = ATGETCELLS(RING, 'field', @TESTFUNCTION, ARGS...)
0014 %   Uses the user-defined TESTFUNCTION to select array elements
0015 %   TESTFUNCTION must be of the form:
0016 %       OK=TESTFUNTION(ATELEM,FIELDVALUE,ARGS...)
0017 %
0018 % OK is a logical array with the same size as RING, refering to matching
0019 % elements in RING
0020 %
0021 % See also ATGETFIELDVALUES, ATSETFIELDVALUES, FINDCELLS, REGEXPI
0022 
0023 % Check if the first argument is the cell array of structures
0024 if ~iscell(cellarray)
0025     error('The first argument must be a cell array of structures')
0026 end
0027 % Check if the second argument is a string
0028 if(~ischar(field))
0029     error('The second argument must be a character string')
0030 end
0031 
0032 if nargin<3
0033     tesfunc=@(elem,field) true;
0034     vals={};
0035 elseif isa(varargin{1},'function_handle')
0036     tesfunc=varargin{1};
0037     vals=varargin(2:end);
0038 else
0039     tesfunc=@defaultfunc;
0040     vals=varargin;
0041 end
0042 
0043 ok=cellfun(@(elem) isfield(elem,field) && tesfunc(elem,elem.(field),vals{:}), cellarray);
0044 
0045     function ok=defaultfunc(el,fieldval,varargin) %#ok<INUSL>
0046         ok=false;
0047         if ischar(fieldval)
0048             %ok=any(cellfun(@charcompare,varargin))
0049             for j=1:length(varargin)
0050                 if ischar(varargin{j}) && ~isempty(regexpi(fieldval,['^' varargin{j} '$']))
0051                     ok=true;
0052                     break;
0053                 end
0054             end
0055         elseif isnumeric(fieldval)
0056             %ok=any(cellfun(@numcompare,varargin))
0057             for j=1:length(varargin)
0058                 if isnumeric(varargin{j}) && fieldval==varargin{j}
0059                     ok=true;
0060                     break;
0061                 end
0062             end
0063         end
0064 %         function ok=charcompare(val)
0065 %             ok=ischar(val) && ~isempty(regexpi(fieldval,['^' val '$']));
0066 %         end
0067 %         function ok=numcompare(val)
0068 %             ok=isnumeric(val) && fieldval==val;
0069 %         end
0070     end
0071 
0072 end

Generated on Thu 24-Aug-2017 18:47:33 by m2html © 2005