rename field name structure in matlab

To rename a field name in a structure in MATLAB, you can use the dot notation and the fieldnames() method to create a new copy of the structure with the new field name as shown here:

main.m
% sample data
data = struct('a', [1 2 3], 'b', [4 5 6]);
% rename the field name 'a' as 'newname'
data.newname = data.a;
data = rmfield(data, 'a');
148 chars
6 lines

Here, we create a new field named 'newname' and set its value to the value of the original 'a' field. Then, we remove the original 'a' field using the rmfield() method. Now, the structure data has a new field name 'newname' with the same values as the original 'a' field.

related categories

gistlibby LogSnag