Dynamic field names in MATLAB - Time & Space Complexity
We want to see how the time needed to run code with dynamic field names changes as the input grows.
How does using dynamic field names affect the speed when we add more fields?
Analyze the time complexity of the following code snippet.
fields = {'a', 'b', 'c', 'd', 'e'};
S = struct();
for i = 1:length(fields)
fieldName = fields{i};
S.(fieldName) = i * 10;
end
This code creates a structure and adds fields using names stored in a list, one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of field names and adding each as a field to the structure.
- How many times: Once for each field name in the list (n times).
Each new field is added one after another, so the time grows as we add more fields.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 field additions |
| 100 | About 100 field additions |
| 1000 | About 1000 field additions |
Pattern observation: The time needed grows directly with the number of fields added.
Time Complexity: O(n)
This means the time to add fields grows in a straight line as the number of fields increases.
[X] Wrong: "Adding fields with dynamic names is instant no matter how many fields there are."
[OK] Correct: Each new field requires some work, so more fields mean more time.
Understanding how dynamic field names affect speed helps you write clear and efficient code, a skill useful in many coding tasks.
"What if we used a nested loop to add fields inside fields? How would the time complexity change?"
Practice
Solution
Step 1: Understand dynamic field names
Dynamic field names allow using a variable to specify the name of a field in a structure.Step 2: Identify the purpose
This helps when field names are not fixed and need to be set or accessed dynamically during program execution.Final Answer:
To use variables as field names in structures -> Option AQuick Check:
Dynamic field names = variable field names [OK]
- Confusing dynamic fields with arrays
- Thinking dynamic fields define functions
- Mixing dynamic fields with matrix operations
fname in a structure S?Solution
Step 1: Recall MATLAB syntax for dynamic fields
Dynamic field names use parentheses and dot notation: structure.(variableName).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.Final Answer:
S.(fname) = 10; -> Option AQuick Check:
Use dot and parentheses for dynamic fields [OK]
- Using dot without parentheses for variable field
- Using curly braces or square brackets instead of dot
- Confusing cell or array indexing with struct fields
field = 'age'; S.(field) = 25; disp(S.age);
Solution
Step 1: Assign dynamic field
The variablefieldholds 'age'. UsingS.(field) = 25;creates field 'age' with value 25 in structure S.Step 2: Display the field value
disp(S.age);prints the value of field 'age', which is 25.Final Answer:
25 -> Option DQuick Check:
Dynamic field 'age' = 25 [OK]
- Expecting field name to be literal 'field'
- Confusing variable name with field name
- Not using parentheses for dynamic field assignment
fname = 'score'; S.fname = 100; disp(S.score);
Solution
Step 1: Analyze field assignment
The code usesS.fname = 100;which creates a field literally named 'fname', not 'score'.Step 2: Check display statement
disp(S.score);tries to display field 'score' which does not exist, causing an error.Final Answer:
Using dot notation with variable name instead of dynamic field -> Option CQuick Check:
Use S.(fname) for dynamic fields [OK]
- Using dot notation without parentheses for variable fields
- Assuming variable name becomes field name automatically
- Ignoring error from missing field access
fields = {'name', 'age', 'score'} and values vals = {'Alice', 30, 95}, which code correctly creates a structure S with these dynamic fields and values?Solution
Step 1: Understand dynamic assignment in loop
We want to assign each field name fromfieldsas a field inSwith corresponding value fromvals.Step 2: Check each option
for i = 1:length(fields) S.(fields{i}) = vals{i}; end usesS.(fields{i}) = vals{i};which correctly assigns dynamic fields. Options A, B, and D use incorrect syntax or swap keys and values.Final Answer:
for i = 1:length(fields) S.(fields{i}) = vals{i}; end -> Option BQuick Check:
Loop with S.(fields{i}) = vals{i} assigns dynamic fields [OK]
- Using dot notation without parentheses inside loop
- Confusing cell array indexing with struct field access
- Swapping keys and values in assignment
Ready to prove it?
Pass the test and this pattern moves unknown
