给定结构数组,如何重命名字段?例如,给定以下内容,如何将“bar”更改为“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)
什么是最佳方法,“最佳”是性能,清晰度和通用性之间的平衡?
最佳答案
扩展 Matthew 中的 this solution,如果新旧字段名称存储为字符串,您还可以使用 dynamic field names:
newName = 'baz';
oldName = 'bar';
[a.(newName)] = a.(oldName);
a = rmfield(a,oldName);
关于matlab - 如何在 MATLAB 中重命名结构体数组中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2733582/