Convert Matrix into string

Dear all,
Lets assume I have matrix
A = [1 2 3 4 5;6 7 8 9 10;11 12 13 14 15];
Now I want to write CSV file in following format
1, 2 3 4 5
6, 7 8 9 10
11, 12 13 14 15
first element should be the first column and all remaining the other column separated by space.

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 6 月 9 日

0 投票

fopen / fprintf / fclose

2 件のコメント

Aravin
Aravin 2018 年 6 月 9 日
despite my all efforts, I couldn't remove [ ] from input string.
for i=1:5
fprintf(fid, '%d,%s\n',a(i,1), mat2str( a(i,2:end)));
end
how to remove [ ]
Walter Roberson
Walter Roberson 2018 年 6 月 9 日
I didn't say anything about using mat2str.
fmt = ['%d, ', repmat('%d ', 1, size(A,2)-2), '%d\n'];
fid = fopen('YourOutputFile.csv', 'wt');
fprintf(fid, fmt, A.'); %transpose is important
fclose(fid)

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2018 年 6 月 9 日

コメント済み:

2018 年 6 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by