How do I rename fields of a structure array?

609 Ansichten (letzte 30 Tage)
John Petersen
John Petersen am 17 Mär. 2016
Kommentiert: Leon am 9 Mär. 2024
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
  4 Kommentare
Leon
Leon am 7 Jan. 2024
I third it.
Steven Lord
Steven Lord am 9 Jan. 2024
Bearbeitet: Steven Lord am 9 Jan. 2024
To officially submit this as an enhancement request please contact Technical Support directly using this link. When you do, please mention the circumstances in which you'd want to use this functionality if it existed.
  • Would you need it to support non-scalar struct arrays?
  • Would you need it to be able to rename one field at a time or renaming a collection of fields all at once?
  • How would you expect it to operate if you tried to rename a field to a field name that already exists in the struct? Error, warn and do nothing, overwrite, try to merge them somehow, etc.?
  • Do you need it to be able to operate over multiple levels? If you had a struct a with fields b and c where b was itself a struct array with field d, would you expect to be able to change both a.c and a.b.d in one call (assuming the answer to my second question was "renaming a collection of fields all at once")?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Mär. 2016
struct2cell( cell2struct(A), {'aa', 'ba', 'ca', 'da'})
  5 Kommentare
Guillaume
Guillaume am 18 Mär. 2016
Bearbeitet: Guillaume am 18 Mär. 2016
This is probably the most straightforward method. For safety, as there's no guarantee that the fields in the original structure are ordered alphabetically, I'd use:
cell2struct(struct2cell(ordefields(A)), {'aa', 'ba', 'ca', da'})
Note that this will cope with structure arrays as well.
Christian Svensson
Christian Svensson am 27 Nov. 2019
Bearbeitet: Christian Svensson am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (6)

Jos (10584)
Jos (10584) am 17 Mär. 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 Kommentare
John Petersen
John Petersen am 17 Mär. 2016
Right, this does not work for a structured array. Do you know what 'tweaking' would make it work?
Jos (10584)
Jos (10584) am 18 Mär. 2016
Use deal (see my other answer)

Melden Sie sich an, um zu kommentieren.


Sriram Nayak
Sriram Nayak am 18 Apr. 2020
Bearbeitet: Walter Roberson am 6 Mär. 2024
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
  1 Kommentar
Walter Roberson
Walter Roberson am 6 Mär. 2024
This will fail if the new field names happen to include some of the old field names. For example
a -> aa
b -> a

Melden Sie sich an, um zu kommentieren.


Jos (10584)
Jos (10584) am 18 Mär. 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

Teresa Martinez
Teresa Martinez am 26 Mai 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 am 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:

Leon
Leon am 7 Jan. 2024
Bearbeitet: Leon am 7 Jan. 2024
Until Matlab (hopefully) introduces a rename field function, I would convert to a table then use renamevars() and then convert back to a struct. E.g., where S is your struct:
T = struct2table(S);
T = renamevars(T, 'Old_Name', 'New_Name');
S = table2struct(T);
Or, as one line:
S = table2struct(renamevars(struct2table(S), 'Old_Name', 'New_Name'));
An advantage of this over converting to a cell array and back is that you don't need to worry about the names of the other fieldnames, or know what number the field is, etc. You can still rename several fields at once when using renamevars. Also, since each variable in a Matlab table has to maintain the same value for all rows, it should be much more efficient memory-wise than converting a large struct into a cell array and back (where each cell can be of any type and that type must be stored), but I haven't done any tests.
Actually, once you have converted to a table, you may want to have a good think about whether it makes sense to convert it back into a struct: it is often more comfortable to work with tables, and they can be more efficient memory-wise. Apart from better memory use and functions like renamevars, there are neat features like being able to have several variable names under one column.
Or try the C-Mex "renamefield" on Matlab file exchange, as Jan suggested.
  5 Kommentare
Stephen23
Stephen23 am 9 Mär. 2024
"I'm surpised Matlab isn't smart enough to just keep the columns as cell type."
They aren't cell type.
Leon
Leon am 9 Mär. 2024
I see. Thanks.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by