Bird
0
0

Identify the error in the following MATLAB code snippet:

medium📝 Debug Q14 of 15
MATLAB - Cell Arrays and Structures
Identify the error in the following MATLAB code snippet:
fname = 'score';
S.fname = 100;
disp(S.score);
AVariable fname is not defined
BMissing semicolon after assignment
CUsing dot notation with variable name instead of dynamic field
Ddisp function cannot display structure fields
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes