Bird
0
0

Consider the code:

medium📝 Debug Q14 of 15
MATLAB - Cell Arrays and Structures
Consider the code:
C = {1, 2, 3};
val = C(2);
disp(val + 1);

What is the error in this code?
ACannot add 1 to a cell array; use curly braces to get content.
BIndexing with parentheses is invalid syntax.
CThe cell array C is not defined properly.
Ddisp cannot display numeric values.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the type of val after indexing

    val = C(2); uses parentheses, so val is a 1x1 cell array, not a number.
  2. Step 2: Understand why adding 1 causes error

    Adding 1 to a cell array is invalid. To add 1 to the content, use curly braces: val = C{2};
  3. Final Answer:

    Cannot add 1 to a cell array; use curly braces to get content. -> Option A
  4. Quick Check:

    Parentheses return cell, cannot add number [OK]
Quick Trick: Add numbers only to content, not cells [OK]
Common Mistakes:
  • Using parentheses when curly braces needed
  • Assuming disp cannot show numbers
  • Thinking parentheses indexing is syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes