Type conversion functions change data from one type to another. This helps when you need to work with different kinds of data together.
Type conversion functions in MATLAB
newVar = functionName(oldVar);
Replace functionName with the conversion function like double, int32, char, or logical.
The input variable oldVar is converted to the new type and stored in newVar.
x = int32(5);
y = double(x);str = '123'; num = str2double(str);
val = 3.7;
intVal = int32(val);flag = true; numFlag = double(flag);
This program converts a string to a double number, then to an integer, and finally converts that integer to a character. It prints each step.
str = '45.67'; num = str2double(str); intNum = int32(num); charNum = char(intNum); fprintf('Original string: %s\n', str); fprintf('Converted to double: %.2f\n', num); fprintf('Converted to int32: %d\n', intNum); fprintf('Converted int32 to char: %s\n', charNum);
When converting from floating-point to integer, MATLAB truncates the decimal part (does not round).
Converting numbers to characters uses ASCII codes, so the result may not be a digit.
Use str2double to convert strings to numbers safely; it returns NaN if conversion fails.
Type conversion functions change data types to help with different operations.
Common functions include double, int32, char, and str2double.
Be careful with conversions that may lose information, like float to int or number to char.
Practice
Solution
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.Step 2: Identify the correct conversion function
str2doubleconverts strings to double numbers, whiledoubleconverts numeric types to double,int32converts to 32-bit integers, andcharconverts numbers to characters.Final Answer:
str2double -> Option AQuick Check:
String to double = str2double [OK]
- Using double on a string causes error
- Using int32 converts to integer, not double
- Using char converts to characters, not numbers
x to a 32-bit integer in MATLAB?Solution
Step 1: Recall MATLAB's type conversion syntax
MATLAB uses functions named after the target type, likeint32, called with parentheses around the variable.Step 2: Check each option's syntax
Onlyint32(x)is valid MATLAB syntax. The others are not MATLAB functions or use incorrect syntax.Final Answer:
int32(x) -> Option CQuick Check:
Correct syntax for int32 conversion = int32(x) [OK]
- Using non-existent functions like convertToInt32
- Incorrect function call syntax
- Confusing with other language syntax
x = '123.45';
y = str2double(x);
z = int32(y);
disp(z);
Solution
Step 1: Convert string to double
str2double('123.45')converts the string to numeric double 123.45.Step 2: Convert double to int32
int32(123.45)converts the double to integer by truncating the decimal part, resulting in 123.Final Answer:
123 -> Option DQuick Check:
str2double + int32 truncates decimals = 123 [OK]
- Expecting int32 to round instead of truncate
- Thinking str2double returns string
- Confusing output with original string
x = 65;
y = str2double(x);
disp(y);
Solution
Step 1: Check input type for str2double
str2doubleexpects a string input, butxis numeric (65).Step 2: Identify the cause of error
Passing numericxtostr2doublecauses unexpected behavior or error because it is not a string.Final Answer:
x should be a string for str2double -> Option BQuick Check:
str2double needs string input [OK]
- Passing numeric directly to str2double
- Thinking disp needs semicolon
- Believing disp can't show numbers
data = {'10', '20.5', '30', '40.7'}. Which MATLAB code correctly converts all elements to a numeric array of doubles?Solution
Step 1: Understand data type and conversion needed
datais a cell array of strings; we want a numeric double array.Step 2: Use cellfun with str2double
cellfun(@str2double, data)appliesstr2doubleto each cell element, converting strings to doubles.Step 3: Check other options
double(data)andint32(data)do not work on cell arrays directly;str2double(data)expects a string, not a cell array.Final Answer:
numArray = cellfun(@str2double, data); -> Option AQuick Check:
Use cellfun with str2double for cell string arrays [OK]
- Trying double or int32 on cell arrays directly
- Using str2double on cell array without cellfun
- Not converting strings inside cells individually
Ready to prove it?
Pass the test and this pattern moves unknown
