Matlab

How to Convert Matrix to a String in MATLAB?

Converting a matrix to a string is a fundamental task in MATLAB that allows you to take advantage of the benefits of both matrices and strings. For example, you can use matrices to store large amounts of text data efficiently, then, you can use strings to manipulate and visualize the text data.

Follow this guide to convert the matrix to a string in MATLAB.

How to Convert Matrix to a String in MATLAB?

For MATLAB users, there is a built-in function called mat2str() that allows easy conversion of the matrix to a string. This function uses a single argument in the form of a matrix to quickly convert a matrix to a string. You can also add an argument inside this function to change the mode of the function.

Syntax
There are different syntaxes of mat2str() that serve different purposes. These syntaxes are provided below:

str= mat2str(M)
str= mat2str(M, n)
str= mat2str(M, 'class')
str= mat2str(M, n, 'class')

The str= mat2str(M) converts a matrix M to a string; the elements of the matrix are concatenated together, returning the resulting string.

The str= mat2str(M, n) converts a matrix M to a string with a maximum length of n characters. If the elements of the matrix are longer than n characters, they are truncated.

The str= mat2str(M, ‘class’) converts a matrix M to a string with the specified class. The class can be one of the following:

  • char: The elements of the matrix are converted to characters.
  • double: The elements of the matrix are converted to double-precision floating-point numbers.
  • single: The elements of the matrix are converted to single-precision floating-point numbers.
  • int8: The elements of the matrix are converted to 8-bit signed integers.
  • uint8: The elements of the matrix are converted to 8-bit unsigned integers.

Examples
The following examples demonstrate the working of the mat2str() function in converting the matrix to a string in MATLAB.

Example 1: Convert a 3×3 Matrix to a String in MATLAB Using mat2str() Function
The following example creates a 3×3 matrix and then uses the mat2str() function to convert this matrix into a string in MATLAB.

A = magic(3)
str = mat2str(A)

Example 2: Convert a 2×3 Matrix to a String in MATLAB Using mat2str() Function with n characters
The following example creates a 2×3 matrix of floating-point numbers and then uses the mat2str() function to generate a string with a maximum length of 3 characters in each element.

A = [3.6244 2.3355 9.86664 ; 1.27653 3.28924 1.126489]
str = mat2str(A,3)

Conclusion

Converting a matrix to a string is a fundamental task and can be performed easily using the built-in mat2str() function. The mat2str() function has different syntaxes and can be used based on specific needs. In the above guide, we have provided the basics of converting a matrix to a string using the mat2str() function in MATLAB. The examples are provided too, which include using the mat2str() function simply in a 3×3 matrix and using the mat2str() function with n characters in a 2×3 floating-point matrix.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.