HOWTO · MATLAB
How to Convert Number to String in MATLAB
Learn how to convert MATLAB numbers to text with num2str(), int2str(), and string().
On this page
To convert a numeric value to character text in MATLAB, use num2str(); use int2str() for integer values or string() when you need MATLAB string data. For example, num2str(42) produces the character text 42. The choice matters when you format decimals or work with arrays because the functions return different kinds of text data.
Convert a Number to a String in MATLAB
Choose the result type based on what the next operation needs: num2str() and int2str() return character arrays, while string() returns MATLAB string data. Character arrays are useful with older character-oriented functions and formatting operations; string arrays are useful when the rest of your code expects MATLAB strings.
The executable examples below use Octave-compatible syntax shared with MATLAB. The num2str() example demonstrates scalar numeric-to-character conversion, and the int2str() example demonstrates integer-to-character conversion; both appear under the sections that explain those functions. The string() section describes MATLAB string data separately because Octave support is not assumed here. The two Octave examples in the num2str() and int2str() sections were executed and passed, producing 42 and -23, respectively. The string() syntax illustration is MATLAB-specific and was not runtime-verified by the supplied evidence.
Use num2str() for Numeric Values and Formatting
Use num2str() for general numeric values, including integers, decimals, and arrays. Its basic form is num2str(A). To control a scalar’s displayed format, pass a format specification such as num2str(x, '%.2f'); to control significant digits, pass a precision such as num2str(x, 4). These options have different meanings: a format specification controls the printed pattern, while the numeric precision argument sets significant digits. For fixed decimal places, a format such as '%.2f' is usually clearer.
Run this example:
value = 42;
textValue = num2str(value);
disp(textValue);
42
textValue is character data containing the digits. For a decimal, num2str(3.14159) produces text such as 3.1416 with the default formatting. Supply a format specification to control decimal places: num2str(3.14159, '%.2f') produces 3.14. The numeric second-argument form controls significant digits: num2str(3.14159, 4) produces 3.142. These are different controls: '%.2f' requests two digits after the decimal point, while 4 requests four significant digits. These added cases are syntax and result illustrations; only the 42 example above is covered by the supplied runtime evidence.
For a numeric array, num2str([1.25 2.5]) produces character text like 1.25 2.5, with spaces separating the values; a matrix input produces multiple rows of character data. It does not create a MATLAB string array with one element per number. You can use num2str() for scientific notation too; the chosen representation depends on the value and formatting options, so avoid relying on a particular exponent spelling when it matters. Specify a format to make the representation explicit. The array case is also an unexecuted illustration here.
To select a digit by position, first convert the number to character data and then index that character array. For example, digits = num2str(9876); secondDigit = digits(2); selects the character '8'. This is character indexing: secondDigit is a character, not a numeric value. If subsequent arithmetic is needed, convert it separately; str2num() parses numeric text, though for a single digit str2double() is a more direct parsing choice.
Use int2str() for Integer Values
Use int2str() when the input represents integers and a simple character representation is sufficient. It accepts scalar or array integer values and returns character data. For an array, elements are rendered as aligned text in a character array; the spacing is for column alignment, not a delimiter-separated string.
Run this example:
count = -23;
countText = int2str(count);
disp(countText);
-23
For an integer array, int2str([12 345; 6 78]) produces a character array with two rows, corresponding to the two input rows; spaces align the values in each column. This array result is an unexecuted illustration here.
For decimal values, formatted output, or significant-digit control, prefer num2str(). int2str() is not a formatting function and should not be used to preserve a fractional part.
Use string() for MATLAB String Arrays
Use MATLAB’s string() to convert numeric values to string data. For a scalar, string(42) creates the string value "42". For an array, string([42 3.14]) creates a 1-by-2 string array with the values ["42" "3.14"]; each element corresponds to one numeric input, rather than the function returning one character array containing the whole row. This element-wise shape is often convenient for combining values with other strings. These scalar and array results are illustrative MATLAB behavior and were not verified by the supplied runtime evidence.
The examples in this section are MATLAB syntax illustrations, not runtime-verified examples. If you need a string array, check that your MATLAB environment supports the function and confirm the resulting display in that environment; do not treat char() as an equivalent numeric-to-text conversion. char() converts character codes or string data to character arrays, which is a different operation.
Distinguish Numeric Text from Character Codes
Converting a number such as 65 to text gives the digits 65; it does not produce the character A. By contrast, char(65) converts the numeric character code 65 to the corresponding character in the character encoding used by MATLAB. Character-code values and their resulting characters depend on the supported character representation, so this is not a general method for formatting numbers as text.
Keep the tasks separate: use num2str() or int2str() for character text representing numeric values, and use string() when MATLAB string data is required. Index a character array after conversion when you need a digit at a particular position; parse that selected character separately only if a numeric value is needed.