Bird
Raised Fist0
MATLABdata~20 mins

Converting between types in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of MATLAB Type Conversion
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of converting numeric array to logical
What is the output of this MATLAB code?
a = [0 1 2 -3 0];
b = logical(a);
disp(b);
MATLAB
a = [0 1 2 -3 0];
b = logical(a);
disp(b);
A[0 0 0 0 0]
B[false true true true false]
C[false 1 1 1 false]
D[0 1 1 1 0]
Attempts:
2 left
💡 Hint
Logical conversion treats zero as false and nonzero as true.
❓ Predict Output
intermediate
2:00remaining
Result of char to double conversion
What is the output of this MATLAB code?
c = 'MATLAB';
d = double(c);
disp(d);
MATLAB
c = 'MATLAB';
d = double(c);
disp(d);
A[77 65 84 76 65 66]
B['M' 'A' 'T' 'L' 'A' 'B']
C[109 97 116 108 97 98]
D[0 0 0 0 0 0]
Attempts:
2 left
💡 Hint
double converts characters to their ASCII numeric codes.
❓ Predict Output
advanced
2:00remaining
Output of converting numeric to string and back
What is the value of variable y after running this MATLAB code?
x = 123.45;
s = string(x);
y = double(s);
MATLAB
x = 123.45;
s = string(x);
y = double(s);
ANaN
BError: Cannot convert string to double directly
C[49 50 51 46 52 53]
D123.45
Attempts:
2 left
💡 Hint
double applied to a string array returns NaN if string is not numeric array.
❓ Predict Output
advanced
2:00remaining
Effect of converting logical array to double
What is the output of this MATLAB code?
l = [true false true];
d = double(l);
disp(d);
MATLAB
l = [true false true];
d = double(l);
disp(d);
A[2 1 2]
B[true false true]
C[0 0 0]
D[1 0 1]
Attempts:
2 left
💡 Hint
Logical true converts to 1, false to 0 when cast to double.
❓ Predict Output
expert
2:00remaining
Output of converting cell array of mixed types to numeric array
What error or output does this MATLAB code produce?
c = {1, '2', 3};
a = cell2mat(c);
MATLAB
c = {1, '2', 3};
a = cell2mat(c);
A[1 2 3]
B[1 '2' 3]
CError: cell2mat cannot convert cell array with mixed types
D[1 0 3]
Attempts:
2 left
💡 Hint
cell2mat requires all cells to be numeric arrays of compatible sizes.

Practice

(1/5)
1. Which MATLAB function converts a string like '123.45' into a numeric double value?
easy
A. char
B. int32
C. str2double
D. num2str

Solution

  1. Step 1: Understand the input and desired output

    The input is a string representing a number, and we want a numeric double value.
  2. Step 2: Identify the correct conversion function

    str2double converts strings to double precision numbers, while int32 converts numbers to integers, char converts numbers to characters, and num2str converts numbers to strings.
  3. Final Answer:

    str2double -> Option C
  4. Quick Check:

    String to double = str2double [OK]
Hint: Use str2double for string to number conversion [OK]
Common Mistakes:
  • Using int32 on strings causes errors
  • Using char converts numbers to characters, not numbers
  • Using num2str converts numbers to strings, not the other way
2. Which of the following is the correct syntax to convert the number 3.7 to an integer in MATLAB?
easy
A. int32(3.7)
B. int(3.7)
C. integer(3.7)
D. toInt(3.7)

Solution

  1. Step 1: Recall MATLAB integer conversion functions

    MATLAB uses functions like int32, int16, etc., to convert numbers to integers.
  2. Step 2: Check syntax correctness

    int32(3.7) is valid syntax; the others are not MATLAB functions.
  3. Final Answer:

    int32(3.7) -> Option A
  4. Quick Check:

    Correct integer conversion = int32(3.7) [OK]
Hint: Use int32() to convert decimals to integers [OK]
Common Mistakes:
  • Using non-existent functions like int() or integer()
  • Forgetting parentheses in function calls
  • Expecting rounding instead of truncation
3. What is the output of the following MATLAB code?
val = int32(5.9);
disp(val);
medium
A. 5
B. Error
C. 5.9
D. 6

Solution

  1. Step 1: Understand int32 conversion behavior

    Converting a decimal number to int32 truncates the decimal part, it does not round.
  2. Step 2: Apply truncation to 5.9

    Truncating 5.9 results in 5, so val becomes 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    int32 truncates decimals = 5 [OK]
Hint: int32 cuts decimals, does not round [OK]
Common Mistakes:
  • Assuming int32 rounds up
  • Expecting output to be 5.9 (no conversion)
  • Thinking it causes an error
4. The following MATLAB code throws an error. What is the problem?
num = '123';
val = int32(num);
medium
A. Variable name 'num' is reserved
B. int32 cannot convert strings directly
C. Missing semicolon after assignment
D. int32 only works with integers, not strings

Solution

  1. Step 1: Analyze the input type

    The variable num is a string, not a numeric value.
  2. Step 2: Check int32 input requirements

    int32 expects numeric input, not strings. To convert a string to a number, use str2double first.
  3. Final Answer:

    int32 cannot convert strings directly -> Option B
  4. Quick Check:

    int32 needs numeric input [OK]
Hint: Convert string to number before int32 [OK]
Common Mistakes:
  • Trying to convert string directly with int32
  • Ignoring type mismatch errors
  • Assuming int32 rounds strings
5. You have a cell array of strings representing numbers: {'10.5', '20.1', '30.9'}. Which MATLAB code correctly converts this to an integer array by truncating decimals?
hard
A. intArray = str2double(int32(cellArray));
B. intArray = int32(cellArray);
C. intArray = int32(str2num(cellArray));
D. intArray = int32(cellfun(@str2double, cellArray));

Solution

  1. Step 1: Understand cell array conversion

    Each string in the cell array must be converted individually to a number.
  2. Step 2: Use cellfun with str2double and then int32

    cellfun(@str2double, cellArray) converts each string to double, then int32 truncates decimals for the whole array.
  3. Step 3: Check other options

    intArray = str2double(int32(cellArray)); tries to convert the cell array to int32 first, which fails. intArray = int32(cellArray); tries int32 on strings directly, which errors. intArray = int32(str2num(cellArray)); uses str2num incorrectly on cell array.
  4. Final Answer:

    intArray = int32(cellfun(@str2double, cellArray)); -> Option D
  5. Quick Check:

    Use cellfun + str2double + int32 [OK]
Hint: Use cellfun with str2double before int32 [OK]
Common Mistakes:
  • Trying to convert whole cell array at once
  • Using int32 directly on strings
  • Confusing str2num with str2double on cell arrays
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