How to write a matrix (4 x 3) into a csv file

1 回表示 (過去 30 日間)
Jake
Jake 2016 年 10 月 18 日
コメント済み: Walter Roberson 2016 年 10 月 21 日
Hi MATLAB experts, I want to write a cell matrix into a csv file as you can see below, called 'output'. At the moment, I am using a function of fprintf with for loop, which has no issues to use as is.
However, in a csv file, I also want to have a header on top of the matrix such as head
header = {'date', 'Name', 'Score};
output =
[42661] [1x40 char] [200]
[42661] [1x40 char] [201]
[42661] [1x40 char] [202]
.
.
.
.
[42661] [1x40 char] [210]
fid=fopen(['C:\MATLAB\output.csv'],'w');
for z = 1:size(output,1)
fprintf(fid, '%d,%s,%d\n', output {z,1}, output {z,1}, output {z,1});
end
fclose(fid);
How can I add the header to this matrix to write into a csv file??
Thanks for your help in advance. Sungho

採用された回答

Walter Roberson
Walter Roberson 2016 年 10 月 18 日
After the fopen,
fprintf(fid, '%s,%s,%s/n', header{:} ) ;
  3 件のコメント
Jake
Jake 2016 年 10 月 21 日
Thanks!! This works perfect. However, when writing numbers with decimals, MATLAB writes those numbers with decimals as Scientific instead of General which I want to keep in the spreadsheet.
How can I do to keep the numbers with decimals in General????
Thanks for your help.
Walter Roberson
Walter Roberson 2016 年 10 月 21 日
Change the %d to %g

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJava Package Integration についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by