📖 Scenario: Imagine you are organizing information about different fruits and their prices. You want to store this information in a MATLAB structure where each fruit name is a field, and the value is its price.
🎯 Goal: You will create a MATLAB structure with fruit names as dynamic field names and assign prices to each fruit. Then, you will display the price of a specific fruit using dynamic field names.
📋 What You'll Learn
Create a structure called fruitPrices with dynamic field names
Use a variable fruitName to hold the name of the fruit
Assign prices to the fields dynamically using fruitName
Display the price of the fruit stored in fruitName
💡 Why This Matters
🌍 Real World
Dynamic field names help when you want to store data with keys that are not known until the program runs, like user input or data from files.
💼 Career
Understanding dynamic field names is useful for data organization and manipulation in MATLAB, common in engineering and scientific programming jobs.
Progress0 / 4 steps
1
Create an empty structure called fruitPrices
Create an empty structure called fruitPrices to hold fruit prices.
MATLAB
Hint
Use struct() to create an empty structure in MATLAB.
2
Create a variable fruitName with the value 'apple'
Create a variable called fruitName and set it to the string 'apple'.
MATLAB
Hint
Use single quotes to create a string in MATLAB.
3
Add a field to fruitPrices using fruitName and assign the price 3.5
Use dynamic field names to add a field to fruitPrices with the name stored in fruitName and assign it the value 3.5.
MATLAB
Hint
Use parentheses and dot notation like structure.(variable) to create dynamic field names.
4
Display the price of the fruit stored in fruitName
Write a disp statement to display the price of the fruit stored in fruitName from the fruitPrices structure.
MATLAB
Hint
Use disp(fruitPrices.(fruitName)) to display the value.
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
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 A
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
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 A
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
Step 1: Assign dynamic field
The variable field holds 'age'. Using S.(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 D
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
Step 1: Analyze field assignment
The code uses S.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 C
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
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.
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.
Final Answer:
for i = 1:length(fields)
S.(fields{i}) = vals{i};
end -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.