Problems writing to text files
5 views (last 30 days)
Show older comments
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 Comments
Answers (1)
per isakson
on 11 Jul 2012
Edited: per isakson
on 11 Jul 2012
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!
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!