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.