Bird
0
0

What will be the output of this MATLAB code?

medium📝 Predict Output Q5 of 15
MATLAB - Cell Arrays and Structures
What will be the output of this MATLAB code?
fields = {'a', 'b', 'c'};
for k = 1:3
  S.(fields{k}) = k*10;
end
disp(S.b);
A20
B10
C30
DError: Index exceeds
Step-by-Step Solution
Solution:
  1. Step 1: Loop assigns values to fields a,b,c

    k=1: S.a=10; k=2: S.b=20; k=3: S.c=30;
  2. Step 2: Display S.b value

    disp(S.b) prints 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Loop dynamic fields = S.b = 20 [OK]
Quick Trick: Use curly braces to access cell array elements [OK]
Common Mistakes:
  • Using parentheses instead of curly braces for cell access
  • Confusing field names with loop index
  • Expecting error due to dynamic fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes