How do I rename fields of a structure array?

조회 수: 684(최근 30일)
John Petersen
John Petersen 2016년 3월 17일
댓글: Rhys Williams 2022년 8월 10일
Say I have a structure array such as
A =
1x49 struct array with fields:
a
b
c
d
How do I rename the structure fields? Such as
A =
1x49 struct array with fields:
aa
ba
ca
da
  댓글 수: 2
Rhys Williams
Rhys Williams 2022년 8월 10일
I second that!

댓글을 달려면 로그인하십시오.

답변(7개)

Walter Roberson
Walter Roberson 2016년 3월 17일
struct2cell( cell2struct(A), {'aa', 'ba', 'ca', 'da'})
  댓글 수: 5
Christian Svensson
Christian Svensson 2019년 11월 27일
편집: Christian Svensson 2019년 11월 27일
I encountered this problem recently, for completeness I'll add my solution. Assuming that you want to add the same suffix to all fieldnames, you can use
cell2struct(struct2cell(A), strcat(fieldnames(A), 'a'))
Note that fieldnames and struct2cell return the field names and the values in the same order, so no sorting is needed.

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2016년 3월 17일
There is a somewhat hidden function called renameStructField, that can do the job, perhaps within a loop.
Or use dynamic field names and copy the fields into a new structure. This makes code quite readable and bug proof.
oldnames = {'a','b','c'}
newnames = {'aa','test','cXY'}
for k=1:number(oldnames)
Snew.(newnames{k}) = Sold.(oldnames{k}) ;
end
  댓글 수: 3
Jos (10584)
Jos (10584) 2016년 3월 18일
Use deal (see my other answer)

댓글을 달려면 로그인하십시오.


Sriram Nayak
Sriram Nayak 2020년 4월 18일
function structOut = renamefield(structIn, oldField, newField)
for i = 1:length(structIn)
structIn = setfield(structIn,{i},newField,getfield(structIn(i),oldField));
end
structOut = rmfield(structIn,oldField);
end

Jos (10584)
Jos (10584) 2016년 3월 18일
You can use DEAL and a for-loop to change fieldnames:
% create a structure array, with different fields
for k=1:10,
Sold(k) = struct('a',k,'b',1:k','c',sprintf('test%02d',k)) ;
end
% engine
oldnames = {'a','b','c'}
newnames = {'aa','test','cXY'}
N = numel(Sold)
for k=1:numel(oldnames)
[Snew(1:N).(newnames{k})] = deal(Sold.(oldnames{k})) ;
end

Stephen23
Stephen23 2016년 3월 17일
편집: Stephen23 2016년 3월 17일
Here is a robust four-line solution that works for a non-scalar structure. It uses a Map container to ensure that the fieldnames are correctly allocated, no matter what order they occur in within the old structure:
map = containers.Map({'a','b','c'},{'aNew','bNew','cNew'});
Here is a 1x3 structure to rename the fields of (the field order is not important):
old = struct('c',{7,8,9}, 'a',{1,2,3}, 'b',{4,5,6});
and the code itself to create a new structure:
tmp = reshape(values(map,fieldnames(old)),1,[]);
tmp(2,:) = num2cell(permute(struct2cell(old),[3,1,2]),1);
new = reshape(struct(tmp{:}),size(old));
Checking it in the command window:
>> old
old =
1x3 struct array with fields:
c
a
b
>> [old.b]
ans =
4 5 6
>> new
new =
1x3 struct array with fields:
cNew
aNew
bNew
>> [new.bNew]
ans =
4 5 6

Teresa Martinez
Teresa Martinez 2021년 5월 26일
Create new fields with the same data:
A.aa = A.a;
A.ba = A.b;
and remove the old ones:
A = rmfield(A,'a');
A = rmfield(A,'b');

Jan
Jan 2022년 6월 8일
Although this thread is old, it has a lot of views and it is worth to mention this implementation as fast C-Mex:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by