Convert Array of Float to comma delimited string
조회 수: 28 (최근 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에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!