Problems writing to text files
1 次查看(过去 30 天)
显示 更早的评论
Hello everybody,
I know this is a known issue, but I still have some troubles getting my thing right, even with solutions suggested. I want to export some data to a text file Basically, I have a cell array with headers ( called parts) for my table, and a matrix with values (called Output).
The way I'm doing this now is:
fid = fopen(fullfile('Darab Export','Output File 2.txt'), 'w+');
fprintf(fid,'%s\t',parts{1:end-1}); %# Write all but the last label
fprintf(fid,'%s\n',parts{end});
fclose(fid);
dlmwrite(fullfile('Darab Export','Output File 2.txt'), [Output(1:20,:)],'delimiter','\t','newline', 'pc','precision','%.6f','-append') %Output(1:20,:)
However, in my case, the results is wrong, since the text file contains all the headers, but on the first line, the values are immediately pasted afterwards. I think something is going wrong with the newline part, as this does not appear in my text file. Hope you guys understand my issue, and can help my out.
Thanks in advance!
Roy
0 个评论
回答(1 个)
per isakson
2012-7-11
编辑:per isakson
2012-7-11
I have R2012a 64bit, Windows7. I tried your code with some data.
The resulting file looks ok in Notepad++, but as you describe without line breaks after the header in Notepad.
Notepad++ shows that the header is terminated by LF, char(10), and the data lines are terminated by CRLF, char(13)char(10).
Windows uses LF and DOS used CRLF - if I remember correctly. Matlab doesn't care when reading(?).
>> double(sprintf('\n'))
ans =
10
Welcome in the club of the confused!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!