Complete the code to create a structure with a dynamic field name.
fieldName = 'age'; person.[1] = 25;
In MATLAB, dynamic field names are accessed using parentheses and the field name as a string: person.(fieldName).
Complete the code to read a dynamic field value from a structure.
field = 'score'; value = data.[1];
To access a dynamic field in MATLAB, use parentheses with the field name string: data.(field).
Fix the error in the code to correctly assign a value to a dynamic field.
fieldName = 'height'; person.[1] = 180;
Dynamic field names require parentheses around the variable holding the field name string: person.(fieldName).
Fill both blanks to create a structure with dynamic field names and assign values.
fields = {'name', 'age'};
values = {'Alice', 30};
for i = 1:length(fields)
person.[1] = values{i};
end
% Access the age field dynamically
ageValue = person.[2];Use person.(fields{i}) to assign values dynamically in the loop, and person.(fields{2}) to access the 'age' field.
Fill all three blanks to create a structure with dynamic fields, assign values, and check a condition.
fields = {'height', 'weight', 'age'};
values = [170, 65, 25];
for i = 1:length(fields)
person.[1] = values(i);
end
if person.[2] > 20
status = '[3]';
else
status = 'young';
endAssign values dynamically with person.(fields{i}), check the 'age' field with person.(fields{3}), and set status to 'adult' if age > 20.