Home > atmlab > handy > rnfield.m

rnfield

PURPOSE ^

RNFIELD Rename Structure Fields.

SYNOPSIS ^

function ss=rnfield(s,oldname,newname)

DESCRIPTION ^

RNFIELD Rename Structure Fields.
 RNFIELD(S,OldName,NewName) returns the structure S with the field name
 denoted by the string OldName changed to NewName. Oldname must exist in S.

 If OldName and NewName are cell arrays of strings of equal length, each
 field name in OldName is changed to the corresponding element in NewName.

 Examples:

 RNFIELD(S,fieldnames(S),lower(fieldnames(S))) makes all field names lower
 case as long as they are unique when lowercase

 If S.a=pi; S.b=inf; then 
 RNFIELD(S,{'a' 'c'},{'c' 'd'}) produces S.d=pi; S.b=inf;

 See also ORDERFIELDS, RMFIELD, ISFIELD, FIELDNAMES.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

rnfield.m

SOURCE CODE ^

0001 function ss=rnfield(s,oldname,newname)
0002 %RNFIELD Rename Structure Fields.
0003 % RNFIELD(S,OldName,NewName) returns the structure S with the field name
0004 % denoted by the string OldName changed to NewName. Oldname must exist in S.
0005 %
0006 % If OldName and NewName are cell arrays of strings of equal length, each
0007 % field name in OldName is changed to the corresponding element in NewName.
0008 %
0009 % Examples:
0010 %
0011 % RNFIELD(S,fieldnames(S),lower(fieldnames(S))) makes all field names lower
0012 % case as long as they are unique when lowercase
0013 %
0014 % If S.a=pi; S.b=inf; then
0015 % RNFIELD(S,{'a' 'c'},{'c' 'd'}) produces S.d=pi; S.b=inf;
0016 %
0017 % See also ORDERFIELDS, RMFIELD, ISFIELD, FIELDNAMES.
0018 
0019 % D.C. Hanselman, University of Maine, Orono, ME 04469
0020 % MasteringMatlab@yahoo.com
0021 % Mastering MATLAB 7
0022 % 2006-02-08, 2006-02-09
0023 
0024 if nargin~=3
0025    error('rnfield:IncorrectNumberofInputArguments',...
0026          'Three Input Arguments Required.')
0027 end
0028 if ~isstruct(s)
0029    error('rnfield:IncorrectInputArgument',...
0030          'First Argument Must be a Structure.')
0031 end
0032 if ~(ischar(oldname)||iscellstr(oldname)) &&...
0033       ~(ischar(newname)||iscellstr(newname))
0034    error('rnfield:IncorrectInputArgument',...
0035          'Last Two Arguments Must be Strings or Cell Strings.')
0036 end
0037 if ischar(oldname)   % convert to cell
0038    oldname=cellstr(oldname);
0039 end
0040 if ischar(newname)   % convert to cell
0041    newname=cellstr(newname);
0042 end
0043 nold=length(oldname);
0044 if nold~=length(newname)
0045    error('rnfield:IncorrectInputArgument',...
0046          'OldName and NewName Must Have the Same Number of Elements.')
0047 end
0048 
0049 fnames=fieldnames(s); % get field names of input structure
0050 
0051 for k=1:nold          % do the work and perform error checking
0052    if ~isequal(oldname{k},newname{k}) % no work if oldname==newname
0053       if ~isvarname(newname{k})
0054          error('rnfield:NewFieldNameNotValid',...
0055                'Invalid New Field Variable Name: %s',newname{k})
0056       end
0057       old=strcmp(fnames,oldname{k});
0058       if ~any(old)
0059          error('rnfield:OldFieldNameDoesNotExist',...
0060                'Structure Does Not Contain the Field: %s',oldname{k})
0061       end
0062       if any(strcmp(fnames,newname{k}))
0063          error('rnfield:NewFieldNameAlreadyExists',...
0064                'Structure Already Contains the Field: %s',newname{k})
0065       end
0066       fnames(old)=newname(k); % replace old field name with new one
0067    end
0068 end
0069 ssize=size(s);
0070 c=struct2cell(s);                          % convert structure to cell
0071 ss=reshape(cell2struct(c,fnames,1),ssize); % rebuild with revised fields

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005