Borrar filtros
Borrar filtros

Problems writing to text files

2 visualizaciones (últimos 30 días)
Roy Veldhuizen
Roy Veldhuizen el 11 de Jul. de 2012
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

Respuestas (1)

per isakson
per isakson el 11 de Jul. de 2012
Editada: per isakson el 11 de Jul. de 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!
  1 comentario
Roy Veldhuizen
Roy Veldhuizen el 11 de Jul. de 2012
Thank you for your reply. I solved the issue based on a statement given on http://www.mathworks.nl/help/techdoc/ref/fprintf.html
"If you plan to read the file with Microsoft Notepad, use '\r\n' instead of '\n' to move to a new line. For example, replace the calls to fprintf with the following:
fprintf(fileID,'%6s %12s\r\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\r\n',A);"
So apparently, Notepad uses a different line break identifier (than notepad plus and others).
Adding \r\n instead of \b solved the issue.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by