String to Number Conversion in MATLAB (str2num)

matlab
string
number
conversion
str2num

This page provides MATLAB source code demonstrating the str2num function, which converts a string representation of a matrix into a numerical matrix. Essentially, it takes a character array and transforms it into a matrix containing numbers.

The str2num function is quite handy for processing data where numbers are stored as strings.

Example:

Let’s say you have the following string matrix:

input = ['10 20'
         '30 40'];

Using str2num like this:

output=str2num(input)

Will produce the following numerical matrix:

output =

    10    20
    30    40

Key Takeaways:

  • str2num converts a string representation to a numerical matrix.
  • It’s useful for processing data stored as strings that need to be used in numerical computations.