0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 function ss=rnfield(s,oldname,newname)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %RNFIELD Rename Structure Fields.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % RNFIELD(S,OldName,NewName) returns the structure S with the field name
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % denoted by the string OldName changed to NewName. Oldname must exist in S.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % If OldName and NewName are cell arrays of strings of equal length, each
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 % field name in OldName is changed to the corresponding element in NewName.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % Examples:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % RNFIELD(S,fieldnames(S),lower(fieldnames(S))) makes all field names lower
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % case as long as they are unique when lowercase
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % If S.a=pi; S.b=inf; then
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % RNFIELD(S,{'a' 'c'},{'c' 'd'}) produces S.d=pi; S.b=inf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % See also ORDERFIELDS, RMFIELD, ISFIELD, FIELDNAMES.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % D.C. Hanselman, University of Maine, Orono, ME 04469
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 % MasteringMatlab@yahoo.com
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 % Mastering MATLAB 7
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % 2006-02-08, 2006-02-09
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 if nargin~=3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 error('rnfield:IncorrectNumberofInputArguments',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 'Three Input Arguments Required.')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 if ~isstruct(s)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 error('rnfield:IncorrectInputArgument',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 'First Argument Must be a Structure.')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 if ~(ischar(oldname)||iscellstr(oldname)) &&...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 ~(ischar(newname)||iscellstr(newname))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 error('rnfield:IncorrectInputArgument',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 'Last Two Arguments Must be Strings or Cell Strings.')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 if ischar(oldname) % convert to cell
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 oldname=cellstr(oldname);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 if ischar(newname) % convert to cell
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 newname=cellstr(newname);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 nold=length(oldname);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 if nold~=length(newname)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 error('rnfield:IncorrectInputArgument',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 'OldName and NewName Must Have the Same Number of Elements.')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 fnames=fieldnames(s); % get field names of input structure
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 for k=1:nold % do the work and perform error checking
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 if ~isequal(oldname{k},newname{k}) % no work if oldname==newname
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 if ~isvarname(newname{k})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 error('rnfield:NewFieldNameNotValid',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 'Invalid New Field Variable Name: %s',newname{k})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 old=strcmp(fnames,oldname{k});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 if ~any(old)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 error('rnfield:OldFieldNameDoesNotExist',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 'Structure Does Not Contain the Field: %s',oldname{k})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 if any(strcmp(fnames,newname{k}))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 error('rnfield:NewFieldNameAlreadyExists',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 'Structure Already Contains the Field: %s',newname{k})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 fnames(old)=newname(k); % replace old field name with new one
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 ssize=size(s);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 c=struct2cell(s); % convert structure to cell
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 ss=reshape(cell2struct(c,fnames,1),ssize); % rebuild with revised fields |