Introduction
Dynamic field names let you create or access parts of a structure using names stored in variables. This helps when you don't know the field names before running the program.
value = structName.(fieldNameVariable); % Access field structName.(fieldNameVariable) = value; % Assign field
fieldName = 'age'; person.(fieldName) = 30;
field = 'score';
value = data.(field);fields = {'height', 'weight'};
for i = 1:length(fields)
stats.(fields{i}) = i * 10;
endperson = struct(); fieldName = 'name'; person.(fieldName) = 'Alice'; fieldName = 'age'; person.(fieldName) = 28; % Display the structure disp(person);
fname in a structure S?field = 'age'; S.(field) = 25; disp(S.age);
field holds 'age'. Using S.(field) = 25; creates field 'age' with value 25 in structure S.disp(S.age); prints the value of field 'age', which is 25.fname = 'score'; S.fname = 100; disp(S.score);
S.fname = 100; which creates a field literally named 'fname', not 'score'.disp(S.score); tries to display field 'score' which does not exist, causing an error.fields = {'name', 'age', 'score'} and values vals = {'Alice', 30, 95}, which code correctly creates a structure S with these dynamic fields and values?fields as a field in S with corresponding value from vals.S.(fields{i}) = vals{i}; which correctly assigns dynamic fields. Options A, B, and D use incorrect syntax or swap keys and values.Pass the test and this pattern moves unknown