How do I rename fields of a structure array?

673 views (last 30 days)
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 Comments

Sign in to comment.

Answers (7)

Jos (10584)
Jos (10584) on 17 Mar 2016
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 Comments

Sign in to comment.


Sriram Nayak
Sriram Nayak on 18 Apr 2020
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

Walter Roberson
Walter Roberson on 17 Mar 2016
struct2cell( cell2struct(A), {'aa', 'ba', 'ca', 'da'})
  5 Comments
Christian Svensson
Christian Svensson on 27 Nov 2019
Edited: Christian Svensson on 27 Nov 2019
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.

Sign in to comment.


Jos (10584)
Jos (10584) on 18 Mar 2016
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 on 17 Mar 2016
Edited: Stephen23 on 17 Mar 2016
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 on 26 May 2021
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 on 8 Jun 2022
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!