0
0
MATLABdata~10 mins

Dynamic field names in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a structure with a dynamic field name.

MATLAB
fieldName = 'age';
person.[1] = 25;
Drag options to blanks, or click blank then click option'
A(fieldName)
BfieldName
C{fieldName}
DfieldName()
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without parentheses for dynamic field names.
Using curly braces instead of parentheses.
2fill in blank
medium

Complete the code to read a dynamic field value from a structure.

MATLAB
field = 'score';
value = data.[1];
Drag options to blanks, or click blank then click option'
A{field}
Bfield
C(field)
D.field
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without parentheses.
Using curly braces instead of parentheses.
3fill in blank
hard

Fix the error in the code to correctly assign a value to a dynamic field.

MATLAB
fieldName = 'height';
person.[1] = 180;
Drag options to blanks, or click blank then click option'
A(fieldName)
BfieldName
C{fieldName}
D['fieldName']
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without parentheses.
Using curly braces or brackets instead of parentheses.
4fill in blank
hard

Fill both blanks to create a structure with dynamic field names and assign values.

MATLAB
fields = {'name', 'age'};
values = {'Alice', 30};
for i = 1:length(fields)
    person.[1] = values{i};
end

% Access the age field dynamically
ageValue = person.[2];
Drag options to blanks, or click blank then click option'
A(fields{i})
B(values{i})
C(fields{2})
D(fields)
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without parentheses inside the loop.
Accessing the field without parentheses.
5fill in blank
hard

Fill all three blanks to create a structure with dynamic fields, assign values, and check a condition.

MATLAB
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';
end
Drag options to blanks, or click blank then click option'
A(fields{i})
B(fields{3})
Cadult
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation without parentheses.
Using wrong field name for the condition.
Forgetting to put quotes around the status string.