Problem 86. Renaming a field in a structure array
MATLAB has a setfield and a rmfield, but not a "rename field". You need to write one for this problem.
Example
Suppose
s(1).foo = 1; s(1).bar = 'one'; s(2).foo = 2; s(2).bar = 'two';
and you want the field "bar" to be renamed "baz". Given the inputs s, oldField, and newField, you must return this output structure with all the data still in place.
s(1).foo = 1; s(1).baz = 'one'; s(2).foo = 2; s(2).baz = 'two';
[Problem from Matthew Simoneau]
Solution Stats
Problem Comments
-
2 Comments
Jan
on 27 Feb 2012
Does "All the data still" mean, that the fields are not re-ordered? A lot of the solutions change the order of the fields.
Rafael S.T. Vieira
on 12 Jun 2020
Currently, Matlab has the function renameStructField.
Solution Comments
Show commentsProblem Recent Solvers1517
Suggested Problems
-
41991 Solvers
-
Back to basics 9 - Indexed References
441 Solvers
-
Return elements unique to either input
769 Solvers
-
8516 Solvers
-
1508 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!