ATSETFIELDVALUES sets the field values of MATLAB cell array of structures Note that the calling syntax must be in the form of assignment: RING = ATSETFIELDVALUES(RING,...) MATLAB does not modify variables that only appear on the right hand side as arguments. NEWRING=ATSETFIELDVALUES(RING,'field',VALUES) In this mode, the function will set values on all the elements of RING NEWRING=ATSETFIELDVALUES(RING,INDEX,'field',VALUES) In this mode, the function will set values on the elements of RING specified by INDEX, given as a list of indices or as a logical mask NEWRING=ATSETFIELDVALUES(RING,'field',VALUESTRUCT) In this mode, the function will set values on the elements of RING whose family names are given by the field names of VALUESTRUCT NEWRING=ATSETFIELDVALUES(RING,RINGINDEX,....,VALUESTRUCT) As in the previous mode, the function will set values on the elements of RING whose family names are given by the field names of VALUESTRUCT. But RINGINDEX=atindex(RING) is provided to avoid multiple computations. Field selection --------------------------------------------------------- NEWRING = ATSETFIELD(RING,'field',VALUES) For each I=1:length(RING), set RING{I}.FIELD=value NEWRING = ATSETFIELD(RING,'field',{M,N},VALUES) For each I=1:length(RING), set RING{I}.FIELD(M,N)=value More generally, NEWRING = ATSETFIELD(RING,subs1,subs2,...,VALUES) For each I=1:length(RING), SETFIELD(RING{I},subs1,subs2,...,value) The first dimension of VALUES must be either length(INDEX) or 1 (the value will be repeated for each element). For a vector to be repeated, enclose it in a cell array. Value format --------------------------------------------------------- Cell array VALUES ----------------- Mx1 or 1xM cell array : one cell per element 1x1 cell array : cell 1 is affected to all selected elements Character array VALUES --------------------- 1xN char array (string) : the string as affected to all selected elements MxN char array : one row per element Numeric array VALUES -------------------- 1x1 (scalar) : the value is affected to all selected elements Mx1 (column) : one value per element MxN (matrix) : one row affected per element. If M==1, the single row is affected to all selected elements To assign column vectors to parameters, use a cell array VALUES, with one column per cell See also ATGETFIELDVALUES ATGETCELLS SETCELLSTRUCT FINDCELLS
0001 function newring = atsetfieldvalues(ring,varargin) 0002 %ATSETFIELDVALUES 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 % RING = ATSETFIELDVALUES(RING,...) 0006 % MATLAB does not modify variables that only appear on the right 0007 % hand side as arguments. 0008 % 0009 %NEWRING=ATSETFIELDVALUES(RING,'field',VALUES) 0010 % In this mode, the function will set values on all the elements of RING 0011 % 0012 %NEWRING=ATSETFIELDVALUES(RING,INDEX,'field',VALUES) 0013 % In this mode, the function will set values on the elements of RING 0014 % specified by INDEX, given as a list of indices or as a logical mask 0015 % 0016 %NEWRING=ATSETFIELDVALUES(RING,'field',VALUESTRUCT) 0017 % In this mode, the function will set values on the elements of RING 0018 % whose family names are given by the field names of VALUESTRUCT 0019 % 0020 %NEWRING=ATSETFIELDVALUES(RING,RINGINDEX,....,VALUESTRUCT) 0021 % As in the previous mode, the function will set values on the elements 0022 % of RING whose family names are given by the field names of VALUESTRUCT. 0023 % But RINGINDEX=atindex(RING) is provided to avoid multiple computations. 0024 % 0025 % Field selection 0026 % --------------------------------------------------------- 0027 % NEWRING = ATSETFIELD(RING,'field',VALUES) 0028 % For each I=1:length(RING), set RING{I}.FIELD=value 0029 % 0030 % NEWRING = ATSETFIELD(RING,'field',{M,N},VALUES) 0031 % For each I=1:length(RING), set RING{I}.FIELD(M,N)=value 0032 % 0033 % More generally, 0034 % NEWRING = ATSETFIELD(RING,subs1,subs2,...,VALUES) 0035 % For each I=1:length(RING), SETFIELD(RING{I},subs1,subs2,...,value) 0036 % 0037 % The first dimension of VALUES must be either length(INDEX) or 1 (the value 0038 % will be repeated for each element). For a vector to be repeated, enclose 0039 % it in a cell array. 0040 % 0041 % Value format 0042 % --------------------------------------------------------- 0043 % Cell array VALUES 0044 % ----------------- 0045 % Mx1 or 1xM cell array : one cell per element 0046 % 1x1 cell array : cell 1 is affected to all selected elements 0047 % 0048 % Character array VALUES 0049 % --------------------- 0050 % 1xN char array (string) : the string as affected to all selected elements 0051 % MxN char array : one row per element 0052 % 0053 % Numeric array VALUES 0054 % -------------------- 0055 % 1x1 (scalar) : the value is affected to all selected elements 0056 % Mx1 (column) : one value per element 0057 % MxN (matrix) : one row affected per element. If M==1, the single row 0058 % is affected to all selected elements 0059 % To assign column vectors to parameters, use a cell array VALUES, with one 0060 % column per cell 0061 % 0062 % See also ATGETFIELDVALUES ATGETCELLS SETCELLSTRUCT FINDCELLS 0063 0064 if isstruct(varargin{end}) 0065 if isstruct(varargin{1}) 0066 ringidx=varargin{1}; 0067 args=varargin(2:end-1); 0068 else 0069 ringidx=atindex(ring); 0070 args=varargin(1:end-1); 0071 end 0072 newring=ring; 0073 for fn=fieldnames(varargin{end})' 0074 fname=char(fn); 0075 newring(ringidx.(fname))=atsetfield(newring(ringidx.(fname)),... 0076 args{:},varargin{end}.(fname)); 0077 end 0078 elseif islogical(varargin{1}) || isnumeric(varargin{1}) 0079 newring=ring; 0080 newring(varargin{1})=atsetfield(newring(varargin{1}),varargin{2:end}); 0081 else 0082 newring=atsetfield(ring,varargin{:}); 0083 end 0084 0085 function newring = atsetfield(ring,varargin) 0086 if ischar(varargin{end}) 0087 values=cellstr(varargin{end}); 0088 elseif isnumeric(varargin{end}) 0089 values=num2cell(varargin{end},2); 0090 else 0091 values=varargin{end}; 0092 end 0093 if isscalar(values) 0094 values=values(ones(length(ring),1)); 0095 end 0096 newring=cellfun(@(el,value) setfield(el,varargin{1:end-1},squeeze(value)),... 0097 ring,values(:),'UniformOutput',false); %#ok<SFLD> 0098 end 0099 0100 end