Problem writing in data into a csv using fprintf

I have a csv file I have written using csvwrite which already contains 3 lines of strings:
fid=fopen('test.csv','wt');
fprintf(fid,'abs 3.1 \n');
fprintf(fid,'This is a line \n');
fprintf(fid,'ID Date lat lon val \n');
Now I am trying to fill in data under each column with data in m which is a 10 by 4 marix. Column 1 are ids (5 digits), col.2 are dates (I want is to be in yyyymmddhhmm format but currently is in scientific notation in the table), cols. 3 and 4 are coordinates (e.g. 43.45 & -114.53), and col.5 are some values (e.g. 1.2345).
I have a hard time writing data in the csv. I am currently trying the following piece of code, but data fills in not the way it should be. Data are not stored under each header correctly.
for r=1:size(m,1)
fprintf(fid,'%5.1f',m(r,:));
if r~=size(m,1)
fprintf(fid,'\n');
end
end
Any thoughts is greatly appreciated.
Kian

回答 (1 件)

Muthu Annamalai
Muthu Annamalai 2015 年 8 月 10 日
編集済み: Muthu Annamalai 2015 年 8 月 10 日

0 投票

Have you tried csvwrite or dlmwrite ?
doc csvwrite
doc dlmwrite

3 件のコメント

Kian
Kian 2015 年 8 月 10 日
編集済み: Kian 2015 年 8 月 10 日
Thank you for the hints.
I tried:
dlmwrite('test.csv', m, '-append', 'delimiter', '\t');
It writes data into table now in order. But cause the headers to all go into the first top left cell.
Walter Roberson
Walter Roberson 2015 年 8 月 10 日
dlmwrite('test.csv', m, '-append', 'delimiter', '\t', 'precision', '%g');
Kian
Kian 2015 年 8 月 10 日
編集済み: Kian 2015 年 8 月 10 日
Thank you, but didn't work.

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

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2015 年 8 月 10 日

コメント済み:

2015 年 8 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by