Convert Array of Float to comma delimited string
33 ビュー (過去 30 日間)
古いコメントを表示
Hi I would like to convert an array of floats to a single string, with commas separating the numbers, as below:
data = [1 2 3 4];
output = "1, 2, 3, 4"
However, I cannot seem to get the commas implemented - here is my attempt:
data2 = cellstr(num2str(data));
output = strjoin(data2, ', ');
i then get an output of;
'1 2 3 4'
What am I doing wrong?
0 件のコメント
採用された回答
Stephen23
2020 年 11 月 16 日
編集済み: Stephen23
2020 年 11 月 16 日
data = [1,2,3,4];
strjoin(compose("%d",data),", ") % provides formatting control
strjoin(""+data,", ") % default formatting
2 件のコメント
RST
2023 年 8 月 3 日
And for 2D arrays we can do:
data = rand(3,4);
csvData = strjoin( join( compose('%.3f', data), ', '), newline())
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!