Home > lattice > setcellstruct.m

setcellstruct

PURPOSE ^

SETCELLSTRUCT sets the field values of MATLAB cell array of structures

SYNOPSIS ^

function CELLARRAY = setcellstruct(CELLARRAY,field,index,values,varargin)

DESCRIPTION ^

SETCELLSTRUCT sets the field values of MATLAB cell array of structures

   Note that the calling syntax must be in the form of assignment:
   CELLARRAY = SETCELLSTRUCT(CELLARRAY,...)
   MATLAB does not modify variables that only appear on the right
   hand side as arguments.

 Numeric data
 ---------------------------------------------------------
 CELLARRAY = SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE,M,N)
     Sets (M,N) element equal to VALUE when the field data is
   a matrix. The assigned VALUE may be
   1. Scalar numeric value to be written to all CELLARRAY elements
   2. Numeric array of the same length as INDEX array

 CELLARRAY = SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE,M) can be used
   for one dimensional vectors

 CELLARRAY = SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE) is the same as
   SETCELLSTRUCT(CELLARRAY,'field',index,1,1) if the field data
   is a scalar

 Character array
 --------------------------------------------------------------------
 CELLARRAY SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE) is a MATLAB
   cell array of strings when specified fields contain strings.
   The assignment VALUE may be
   1. Character string, 
   2. Character array with the number of rows matching the number of
       elements in INDEX array,
   3. Cell array of strings, with either one element or with the same
       length as index.

 See also ATSETFIELDVALUES GETCELLSTRUCT FINDCELLS

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function CELLARRAY = setcellstruct(CELLARRAY,field,index,values,varargin)
0002 %SETCELLSTRUCT sets the field values of MATLAB cell array of structures
0003 %
0004 %   Note that the calling syntax must be in the form of assignment:
0005 %   CELLARRAY = SETCELLSTRUCT(CELLARRAY,...)
0006 %   MATLAB does not modify variables that only appear on the right
0007 %   hand side as arguments.
0008 %
0009 % Numeric data
0010 % ---------------------------------------------------------
0011 % CELLARRAY = SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE,M,N)
0012 %     Sets (M,N) element equal to VALUE when the field data is
0013 %   a matrix. The assigned VALUE may be
0014 %   1. Scalar numeric value to be written to all CELLARRAY elements
0015 %   2. Numeric array of the same length as INDEX array
0016 %
0017 % CELLARRAY = SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE,M) can be used
0018 %   for one dimensional vectors
0019 %
0020 % CELLARRAY = SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE) is the same as
0021 %   SETCELLSTRUCT(CELLARRAY,'field',index,1,1) if the field data
0022 %   is a scalar
0023 %
0024 % Character array
0025 % --------------------------------------------------------------------
0026 % CELLARRAY SETCELLSTRUCT(CELLARRAY,'field',INDEX,VALUE) is a MATLAB
0027 %   cell array of strings when specified fields contain strings.
0028 %   The assignment VALUE may be
0029 %   1. Character string,
0030 %   2. Character array with the number of rows matching the number of
0031 %       elements in INDEX array,
0032 %   3. Cell array of strings, with either one element or with the same
0033 %       length as index.
0034 %
0035 % See also ATSETFIELDVALUES GETCELLSTRUCT FINDCELLS
0036 
0037 if(~iscell(CELLARRAY) || ~isstruct(CELLARRAY{1}) || isempty(CELLARRAY))
0038     error('The first argument must be a non-empty cell array of structures')
0039 end
0040 % Chechk if the second argument is a string
0041 if(~ischar(field))
0042     error('The second argument ''field'' must be a character string')
0043 end
0044 
0045 % cell array 'mn' here is used as comma separated list
0046 % to pass as an argument to setfield.
0047 if nargin > 4
0048     mn=varargin;
0049 else
0050     mn={1};
0051 end
0052 
0053 if ischar(values)
0054     values=cellstr(values);
0055 end
0056 
0057 selcells=CELLARRAY(index);
0058 NV = length(selcells);
0059 if length(values)==1
0060     values=values(ones(NV,1));
0061 end
0062 
0063 if isnumeric(values)
0064     for I=1:NV
0065         selcells{I} = setfield(selcells{I},field,mn,values(I));
0066     end
0067 elseif iscell(values)
0068     for I=1:NV
0069         selcells{I}.(field)=values{I};
0070     end
0071 else
0072     error('The field data must be numeric or character array');
0073 end
0074 
0075 CELLARRAY(index)=selcells;
0076 
0077

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