If uninitiated in the secrets of (Matlab's) regular expressions: \s stands for any whitespace and the + means 1 or more times. This is needed because the amount of space between the numbers output by num2str varies depending on the length of those numbers.
EDIT: The backspace character is handled differently according to the final "usage" of the string. For instance, it works perfectly when displaying the string in the terminal; however, printing the string to a file does not remove the comma. Moreover, the byte array corresponding to the string retains both the comma and the backspace character.
More elegant, one liner:
n = [12345 6789 10234 3452]
n = 1×4
12345 6789 10234 3452
n_str = sprintf("%s\b", sprintf("%i,", n))
n_str = "12345,6789,10234,3452"
The logic:
The nested sprintf creates a string of comma-separated values, but has a trailing comma
The top-level sprintf prints the generated strings and the \b character (backspace) removes the trailing comma
It looks fine when displayed on screen but the internal string has 2 extra chararters of code 44 ',' and 8 backspash at the end. Not sure what side effect it has when precessing later.
I will edit the original answer because it seems that the behavior depends on the final usage of the string. For instance, fprintf-ing to a file does NOT work! The comma is retained.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.