matlab - 如何在 MATLAB 中重命名结构体数组中的字段?

标签 matlab field rename matlab-struct

给定结构数组,如何重命名字段?例如,给定以下内容,如何将“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/

相关文章:

matlab - Matlab 对 mnrfit 不正确吗?

matlab - 我是否以正确的方式使用傅立叶变换?

MySQL 主键字段类型

android - 如何使用 Android 应用程序重命名 sdcard 上的文件?

java - 我将 matlab 代码构建到 Java 项目中,现在在运行调用该 matlab 函数的 java 代码行时遇到错误

Matlab cell2mat错误: all contents of the input cell array must be of the same data type

javascript - 左侧赋值无效

mysql - 从文本文件中的术语列表创建 MySQL 表

file - 无法使用 QDir::rename() 重命名 Qt 中的文件

Linux重命名错误恢复