Bird
0
0

What is the issue with the following MATLAB code?

medium📝 Debug Q6 of 15
MATLAB - Cell Arrays and Structures
What is the issue with the following MATLAB code?
numStr = '250'; val = int16(numStr);
Aint16 does not support values above 127
Bint16 cannot convert strings directly; use str2double first
CThe variable name 'numStr' is invalid
Dint16 requires input to be a character array
Step-by-Step Solution
Solution:
  1. Step 1: Understand int16 input requirements

    The int16 function expects numeric input, not strings.
  2. Step 2: Identify the error

    Passing a string like '250' directly to int16 causes an error because it cannot convert strings to integers.
  3. Step 3: Correct approach

    First convert the string to a numeric type using str2double, then convert to int16: val = int16(str2double(numStr));
  4. Final Answer:

    int16 cannot convert strings directly; use str2double first -> Option B
  5. Quick Check:

    Convert strings to numbers before int16 conversion [OK]
Quick Trick: Convert strings to numbers before int16 conversion [OK]
Common Mistakes:
  • Passing strings directly to int16
  • Assuming int16 can parse strings automatically
  • Ignoring the need for str2double or similar

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes