Number to String Conversion in MATLAB (num2str)

matlab
number
string
conversion
num2str

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), where N specifies 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'