Bird
Raised Fist0
MATLABdata~20 mins

Type conversion functions in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of converting string to double and back
What is the output of this MATLAB code?
MATLAB
x = '123.45';
y = str2double(x);
z = num2str(y);
disp(z);
A123.45
B'123.45'
CError: Conversion failed
D12345
Attempts:
2 left
💡 Hint
str2double converts string to number, num2str converts number back to string.
❓ Predict Output
intermediate
2:00remaining
Result of logical to double conversion
What is the output of this MATLAB code?
MATLAB
a = true;
b = double(a);
disp(b);
Atrue
B1
C0
DError: Cannot convert logical to double
Attempts:
2 left
💡 Hint
Logical true converts to 1 as a double.
❓ Predict Output
advanced
2:00remaining
Output of converting numeric array to char
What is the output of this MATLAB code?
MATLAB
nums = [65 66 67];
chars = char(nums);
disp(chars);
AABC
B[65 66 67]
CError: Cannot convert numeric array to char
Dabc
Attempts:
2 left
💡 Hint
char converts numeric ASCII codes to characters.
❓ Predict Output
advanced
2:00remaining
Result of converting cell array of strings to numeric array
What is the output of this MATLAB code?
MATLAB
c = {'1', '2', '3'};
n = str2double(c);
disp(n);
A{1, 2, 3}
BError: Cannot convert cell array to numeric array
C[1 2 3]
D["1" "2" "3"]
Attempts:
2 left
💡 Hint
str2double converts each string in the cell array to a number.
❓ Predict Output
expert
2:00remaining
Output of converting mixed numeric types to single precision
What is the output of this MATLAB code?
MATLAB
a = int16(1000);
b = double(a);
c = single(b);
disp(class(c));
Adouble
BError: Invalid conversion
Cint16
Dsingle
Attempts:
2 left
💡 Hint
int16 to double to single changes the class accordingly.

Practice

(1/5)
1. Which MATLAB function converts a string representing a number into a numeric double type?
easy
A. str2double
B. double
C. int32
D. char

Solution

  1. Step 1: Understand the input and output types

    The input is a string representing a number, and the output should be a numeric double type.
  2. Step 2: Identify the correct conversion function

    str2double converts strings to double numbers, while double converts numeric types to double, int32 converts to 32-bit integers, and char converts numbers to characters.
  3. Final Answer:

    str2double -> Option A
  4. Quick Check:

    String to double = str2double [OK]
Hint: Use str2double for string to number conversion [OK]
Common Mistakes:
  • Using double on a string causes error
  • Using int32 converts to integer, not double
  • Using char converts to characters, not numbers
2. Which of the following is the correct syntax to convert a numeric variable x to a 32-bit integer in MATLAB?
easy
A. convertToInt32(x)
B. int(x, 32)
C. int32(x)
D. toInt32(x)

Solution

  1. Step 1: Recall MATLAB's type conversion syntax

    MATLAB uses functions named after the target type, like int32, called with parentheses around the variable.
  2. Step 2: Check each option's syntax

    Only int32(x) is valid MATLAB syntax. The others are not MATLAB functions or use incorrect syntax.
  3. Final Answer:

    int32(x) -> Option C
  4. Quick Check:

    Correct syntax for int32 conversion = int32(x) [OK]
Hint: Use function name with parentheses for conversion [OK]
Common Mistakes:
  • Using non-existent functions like convertToInt32
  • Incorrect function call syntax
  • Confusing with other language syntax
3. What is the output of the following MATLAB code?
x = '123.45';
y = str2double(x);
z = int32(y);
disp(z);
medium
A. 123.45
B. '123.45'
C. Error
D. 123

Solution

  1. Step 1: Convert string to double

    str2double('123.45') converts the string to numeric double 123.45.
  2. Step 2: Convert double to int32

    int32(123.45) converts the double to integer by truncating the decimal part, resulting in 123.
  3. Final Answer:

    123 -> Option D
  4. Quick Check:

    str2double + int32 truncates decimals = 123 [OK]
Hint: int32 truncates decimals after str2double [OK]
Common Mistakes:
  • Expecting int32 to round instead of truncate
  • Thinking str2double returns string
  • Confusing output with original string
4. Identify the error in this MATLAB code snippet:
x = 65;
y = str2double(x);
disp(y);
medium
A. str2double cannot convert numeric input
B. x should be a string for str2double
C. Missing semicolon after disp(y)
D. disp cannot display numeric values

Solution

  1. Step 1: Check input type for str2double

    str2double expects a string input, but x is numeric (65).
  2. Step 2: Identify the cause of error

    Passing numeric x to str2double causes unexpected behavior or error because it is not a string.
  3. Final Answer:

    x should be a string for str2double -> Option B
  4. Quick Check:

    str2double needs string input [OK]
Hint: str2double requires string input, not numeric [OK]
Common Mistakes:
  • Passing numeric directly to str2double
  • Thinking disp needs semicolon
  • Believing disp can't show numbers
5. You have a cell array of strings representing numbers: data = {'10', '20.5', '30', '40.7'}. Which MATLAB code correctly converts all elements to a numeric array of doubles?
hard
A. numArray = cellfun(@str2double, data);
B. numArray = double(data);
C. numArray = int32(data);
D. numArray = str2double(data);

Solution

  1. Step 1: Understand data type and conversion needed

    data is a cell array of strings; we want a numeric double array.
  2. Step 2: Use cellfun with str2double

    cellfun(@str2double, data) applies str2double to each cell element, converting strings to doubles.
  3. Step 3: Check other options

    double(data) and int32(data) do not work on cell arrays directly; str2double(data) expects a string, not a cell array.
  4. Final Answer:

    numArray = cellfun(@str2double, data); -> Option A
  5. Quick Check:

    Use cellfun with str2double for cell string arrays [OK]
Hint: Use cellfun(@str2double, cellArray) for cell string to numbers [OK]
Common Mistakes:
  • Trying double or int32 on cell arrays directly
  • Using str2double on cell array without cellfun
  • Not converting strings inside cells individually
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard