Bird
0
0

What will be the output of the following MATLAB code?

medium📝 Predict Output Q13 of 15
MATLAB - Cell Arrays and Structures
What will be the output of the following MATLAB code?
field = 'age';
S.(field) = 25;
disp(S.age);
Aage
BEmpty output
CError: Undefined field 'age'
D25
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes