How to rename a field in a structure array in MATLAB?
Given the array of the structure, how do I rename the field? For example, given the following, how to change "bar" to "baz".
clear
a(1).foo = 1;
a(1).bar = 'one';
a(2).foo = 2;
a(2).bar = 'two';
a(3).foo = 3;
a(3).bar = 'three';
disp(a)
What is the best method when “best” is a balance of performance, clarity and generality?
+2
a source to share
3 answers
Expanding on this solution from Matthew, you can also use dynamic field names if the new and old field names are stored as strings:
newName = 'baz'; oldName = 'bar'; [a.(newName)] = a.(oldName); a = rmfield(a,oldName);
+7
a source to share