Number to String Conversion in MATLAB (num2str)
Advertisement
This page provides MATLAB source code demonstrating number to string conversion using the num2str function. The num2str function converts a numerical matrix into its string representation.
num2str Function Syntax
The general syntax for the num2str function is:
output = num2str(input)
There are also variations with added parameters:
output = num2str(input, N), whereNspecifies the desired digits of precision.output = num2str(input, format), where'format'is a string defining the desired formatting.
Example
Let’s look at a simple example:
input = randn(3,3)
output=num2str(input,5)
In this example, randn(3,3) generates a 3x3 matrix of normally distributed random numbers. The num2str function then converts this matrix into a string representation, with 5 digits of precision. The MATLAB output would be:
output =
'0.17464 -0.58832 0.11393
-0.18671 2.1832 1.0668
0.72579 -0.1364 0.059281'
Explore MATLAB Data Types, Strings & Conversions
- Binary to Decimal Conversion in MATLAB
- Binary to Hex Conversion in MATLAB
- Decimal to Base-N Conversion in MATLAB
- Decimal to Binary Conversion in MATLAB
- Decimal to Hexadecimal Conversion in MATLAB (dec2hex)
- Hex to Binary Conversion in MATLAB
- Hexadecimal String to Double Conversion in MATLAB
- Number to Hexadecimal String Conversion in MATLAB
- Number to String Conversion in MATLAB (num2str)
- String to Double Conversion in MATLAB
- String to Number Conversion in MATLAB (str2num)
- MATLAB Matrix to String (mat2str)
- Base-N Number String to Decimal in MATLAB
