Bird
Raised Fist0
MATLABdata~20 mins

Dynamic field names in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Field Names Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code using dynamic field names?
Consider the following MATLAB code that uses dynamic field names in a struct. What will be the output displayed?
MATLAB
fieldName = 'score';
student.(fieldName) = 95;
disp(student.score);
A95
BError: Undefined function or variable 'score'.
Cstudent.score
D0
Attempts:
2 left
💡 Hint
Dynamic field names allow you to use a variable as the field name in a struct.
❓ Predict Output
intermediate
2:00remaining
What does this code print when using dynamic field names with a loop?
Look at this MATLAB code snippet. What will be the output after running it?
MATLAB
fields = {'a', 'b', 'c'};
for i = 1:length(fields)
    data.(fields{i}) = i*10;
end
disp(data.b);
A20
Bb
C0
DError: Index exceeds matrix dimensions.
Attempts:
2 left
💡 Hint
Each field in 'data' is set to i*10 where i is the loop index.
🔧 Debug
advanced
2:00remaining
Identify the error in this dynamic field name usage
This MATLAB code tries to assign a value using a dynamic field name but causes an error. What is the cause?
MATLAB
field = 123;
record.(field) = 'value';
AError because 'record' is not initialized
BError because 'value' is not defined
CNo error, code runs fine
DError because field name must be a string or character vector
Attempts:
2 left
💡 Hint
Field names in structs must be text, not numbers.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a struct with dynamic field names?
Select the MATLAB code snippet that correctly creates a struct with dynamic field names from variables 'f1' and 'f2'.
Af1 = 'name'; f2 = 'age'; person.f1 = 'Alice'; person.f2 = 30;
Bf1 = 'name'; f2 = 'age'; person.(f1) = 'Alice'; person.(f2) = 30;
Cf1 = 'name'; f2 = 'age'; person{f1} = 'Alice'; person{f2} = 30;
Df1 = 'name'; f2 = 'age'; person.(f1, f2) = {'Alice', 30};
Attempts:
2 left
💡 Hint
Use parentheses and dot notation with a string variable for dynamic field names.
🚀 Application
expert
2:00remaining
How many fields does the struct have after this code runs?
Given the following MATLAB code, how many fields does the struct 'info' contain at the end?
MATLAB
fields = {'x', 'y', 'x', 'z'};
info = struct();
for k = 1:length(fields)
    info.(fields{k}) = k;
end
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Struct fields must be unique; repeated field names overwrite previous values.

Practice

(1/5)
1. What is the main purpose of using dynamic field names in MATLAB structures?
easy
A. To use variables as field names in structures
B. To create arrays with dynamic sizes
C. To perform matrix multiplication
D. To define functions inside structures

Solution

  1. Step 1: Understand dynamic field names

    Dynamic field names allow using a variable to specify the name of a field in a structure.
  2. Step 2: Identify the purpose

    This helps when field names are not fixed and need to be set or accessed dynamically during program execution.
  3. Final Answer:

    To use variables as field names in structures -> Option A
  4. Quick Check:

    Dynamic field names = variable field names [OK]
Hint: Dynamic field names use variables for field keys [OK]
Common Mistakes:
  • Confusing dynamic fields with arrays
  • Thinking dynamic fields define functions
  • Mixing dynamic fields with matrix operations
2. Which of the following is the correct syntax to assign a value to a dynamic field name fname in a structure S?
easy
A. S.(fname) = 10;
B. S.fname = 10;
C. S{fname} = 10;
D. S[fname] = 10;

Solution

  1. Step 1: Recall MATLAB syntax for dynamic fields

    Dynamic field names use parentheses and dot notation: structure.(variableName).
  2. Step 2: Check each option

    S.(fname) = 10; uses S.(fname) which is correct syntax. Options A, C, and D are invalid for dynamic field names.
  3. Final Answer:

    S.(fname) = 10; -> Option A
  4. Quick Check:

    Use dot and parentheses for dynamic fields [OK]
Hint: Use S.(variable) to access dynamic fields [OK]
Common Mistakes:
  • Using dot without parentheses for variable field
  • Using curly braces or square brackets instead of dot
  • Confusing cell or array indexing with struct fields
3. What will be the output of the following MATLAB code?
field = 'age';
S.(field) = 25;
disp(S.age);
medium
A. age
B. Empty output
C. Error: Undefined field 'age'
D. 25

Solution

  1. Step 1: Assign dynamic field

    The variable field holds 'age'. Using S.(field) = 25; creates field 'age' with value 25 in structure S.
  2. Step 2: Display the field value

    disp(S.age); prints the value of field 'age', which is 25.
  3. Final Answer:

    25 -> Option D
  4. Quick Check:

    Dynamic field 'age' = 25 [OK]
Hint: Dynamic field names create fields with variable keys [OK]
Common Mistakes:
  • Expecting field name to be literal 'field'
  • Confusing variable name with field name
  • Not using parentheses for dynamic field assignment
4. Identify the error in the following MATLAB code snippet:
fname = 'score';
S.fname = 100;
disp(S.score);
medium
A. Variable fname is not defined
B. Missing semicolon after assignment
C. Using dot notation with variable name instead of dynamic field
D. disp function cannot display structure fields

Solution

  1. Step 1: Analyze field assignment

    The code uses S.fname = 100; which creates a field literally named 'fname', not 'score'.
  2. Step 2: Check display statement

    disp(S.score); tries to display field 'score' which does not exist, causing an error.
  3. Final Answer:

    Using dot notation with variable name instead of dynamic field -> Option C
  4. Quick Check:

    Use S.(fname) for dynamic fields [OK]
Hint: Use parentheses for dynamic field names to avoid errors [OK]
Common Mistakes:
  • Using dot notation without parentheses for variable fields
  • Assuming variable name becomes field name automatically
  • Ignoring error from missing field access
5. Given a list of field names fields = {'name', 'age', 'score'} and values vals = {'Alice', 30, 95}, which code correctly creates a structure S with these dynamic fields and values?
hard
A. for i = 1:length(fields) S{fields{i}} = vals{i}; end
B. for i = 1:length(fields) S.(fields{i}) = vals{i}; end
C. for i = 1:length(fields) S.fields{i} = vals{i}; end
D. for i = 1:length(fields) S.(vals{i}) = fields{i}; end

Solution

  1. Step 1: Understand dynamic assignment in loop

    We want to assign each field name from fields as a field in S with corresponding value from vals.
  2. Step 2: Check each option

    for i = 1:length(fields) S.(fields{i}) = vals{i}; end uses S.(fields{i}) = vals{i}; which correctly assigns dynamic fields. Options A, B, and D use incorrect syntax or swap keys and values.
  3. Final Answer:

    for i = 1:length(fields) S.(fields{i}) = vals{i}; end -> Option B
  4. Quick Check:

    Loop with S.(fields{i}) = vals{i} assigns dynamic fields [OK]
Hint: Use S.(fields{i}) = vals{i} in loop for dynamic fields [OK]
Common Mistakes:
  • Using dot notation without parentheses inside loop
  • Confusing cell array indexing with struct field access
  • Swapping keys and values in assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard