Bird
0
0

Identify the error in the following code:

medium📝 Debug Q6 of 15
MATLAB - Cell Arrays and Structures
Identify the error in the following code:
data.Name = {'John', 'Jane'};
data.Age = [30, 25];
disp(data(2).Name);
Adata is not a structure array but a single structure with cell arrays.
BIncorrect use of curly braces for Name field.
CMissing semicolon after disp statement.
DField Age must be a cell array.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze structure creation

    data is a single structure with fields holding arrays, not a structure array.
  2. Step 2: Accessing data(2).Name causes error

    Since data is scalar, data(2) exceeds array bounds.
  3. Final Answer:

    data is not a structure array but a single structure with cell arrays. -> Option A
  4. Quick Check:

    Single struct with arrays ≠ structure array [OK]
Quick Trick: Structure array needs indexing; single struct with arrays does not [OK]
Common Mistakes:
  • Assuming data(2) works on single struct.
  • Confusing cell arrays with structure arrays.
  • Thinking semicolon omission causes error here.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes