How do I convert a numerical vector into a comma-delimited string?

278 ビュー (過去 30 日間)
D.W. Frye
D.W. Frye 2011 年 1 月 10 日
コメント済み: Dyuman Joshi 2023 年 8 月 19 日
Given a numerical vector, how do I convert it into a comma-delimited string?
For example, if
n = [12345 6789 10234 3452]
convert n to
12345,6789,10234,3452

採用された回答

D.W. Frye
D.W. Frye 2011 年 1 月 10 日
n = [12345 6789 10234 3452];
allOneString = sprintf('%.0f,' , n);
allOneString = allOneString(1:end-1);% strip final comma

その他の回答 (6 件)

Michael
Michael 2018 年 6 月 15 日
Have another one-liner:
regexprep(num2str(n),'\s+',',')
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.

EvilDrW
EvilDrW 2015 年 10 月 2 日
編集済み: EvilDrW 2015 年 10 月 2 日
here's another answer, but is a one liner
strjoin(arrayfun(@(x) num2str(x),n,'UniformOutput',false),',')

Jan Siegmund
Jan Siegmund 2020 年 6 月 30 日
Here is another oneliner:
strjoin(cellstr(num2str(n')),',')
Testing all four types with a script
n = randi(100,[1 100]);
tic;
s1 = sprintf('%.0f,' , n);
s1 = s1(1:end-1);
toc
tic;
s2 = strjoin(arrayfun(@(x) num2str(x),n,'UniformOutput',false),',');
toc
tic;
s3 = regexprep(num2str(n),'\s+',',');
toc
tic;
s4 = strjoin(cellstr(num2str(n')),',');
toc
deliveres:
Elapsed time is 0.000248 seconds.
Elapsed time is 0.005549 seconds.
Elapsed time is 0.000746 seconds.
Elapsed time is 0.003352 seconds.
The accepted answer is the fastest.

Dyuman Joshi
Dyuman Joshi 2023 年 8 月 18 日
(Quite) A simple approach that has not been mentioned yet -
n = [12345 6789 10234 3452];
out = strjoin(string(n),",")
out = "12345,6789,10234,3452"
  3 件のコメント
Bruno Luong
Bruno Luong 2023 年 8 月 18 日
編集済み: Bruno Luong 2023 年 8 月 18 日
The first method is not optimal with format '%.0f', for integer better using '%i' (method 6)
n = randi([1e3 1e4],[1 1000]);
t = zeros(1,6);
t(1) = timeit(@() csnum1(n), 1);
t(2) = timeit(@() csnum2(n), 1);
t(3) = timeit(@() csnum3(n), 1);
t(4) = timeit(@() csnum4(n), 1);
t(5) = timeit(@() csnum5(n), 1);
t(6) = timeit(@() csnum6(n), 1);
tms = t*1e3
tms = 1×6
0.3778 8.9497 0.6823 4.0327 0.2052 0.2428
function s1 = csnum1(n)
s1 = sprintf('%.0f,' , n);
s1 = s1(1:end-1);
end
function s2 = csnum2(n)
s2 = strjoin(arrayfun(@(x) num2str(x),n,'UniformOutput',false),',');
end
function s3 = csnum3(n)
s3 = regexprep(num2str(n),'\s+',',');
end
function s4 = csnum4(n)
s4 = strjoin(cellstr(num2str(n')),',');
end
function s5 = csnum5(n)
s5 = strjoin(string(n),',');
end
function s = csnum6(n)
s = sprintf('%i,' , n);
s = s(1:end-1);
end
Dyuman Joshi
Dyuman Joshi 2023 年 8 月 19 日
Even after the change, Method 6 is (marginally) slower than Method 5 (which is the fastest).

サインインしてコメントする。


RST
RST 2023 年 8 月 3 日
We can also use compose() for a formatted one-liner:
a = rand(5,1);
csva = strjoin(compose('%.3f', a), ', ')
csva = '0.580, 0.062, 0.627, 0.295, 0.318'
and also for 2D arrays:
b = rand(3,4);
csvb = strjoin(join(compose( '%.3f', b), ', '), newline())
csvb =
'0.364, 0.708, 0.070, 0.747 0.627, 0.454, 0.729, 0.557 0.389, 0.398, 0.066, 0.471'

Gianfranco Di Domenico
Gianfranco Di Domenico 2023 年 8 月 18 日
編集済み: Gianfranco Di Domenico 2023 年 8 月 18 日
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
  3 件のコメント
Gianfranco Di Domenico
Gianfranco Di Domenico 2023 年 8 月 18 日
I just noticed this behavior.
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.
Bruno Luong
Bruno Luong 2023 年 8 月 18 日
編集済み: Bruno Luong 2023 年 8 月 18 日
Example
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"
str2num(n_str) % oh oh ...
ans = []
str2num(extractBefore(n_str,strlength(n_str)-1))
ans = 1×4
12345 6789 10234 3452
This might create some frustration and pain of debugging

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by