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