fprintf add mysterious rows, dlmwrite works

1 visualización (últimos 30 días)
Josip Strutz
Josip Strutz el 2 de Nov. de 2016
Comentada: Jan el 2 de Nov. de 2016
Hello, I'm trying to write a 2596000x3 matrix to a file, but fprintf add lines at the beginning and the end of the file. dlmwrite instead works perfectly, but it is much slower than printf.
Can somebody tell me what's the reason for this behaviour? I have to proceed hundreds of this files and matrices and a save of a few seconds every file would be great :) This is my matrix (sorry it's too big and for any reason 7zip doesn't want to pack it): http://www57.zippyshare.com/v/L6vEP0XF/file.html
dlmwrite((fullfile(pathname,filename{1})),XYZ,'delimiter','\t') %works perfectly
__________________________________________________
%%add mysterious lines at the beginning and the end
fileID = fopen('test.txt','w');
fprintf(fileID,'%.f %.f %.f\n', XYZ);
fclose(fileID);
  1 comentario
Jan
Jan el 2 de Nov. de 2016
When I try to open you zippyshare link, I'm forwarded to a page for trading stuff. Please find a serious and working way to share your data. I do not want to be piled by commercials.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 2 de Nov. de 2016
Are you sure? Please check the current folder, if the "test.txt" is really written to the location you expect.
If you want to save time, the text format is a bad idea: Note that nobody will be able to edit in such a huge text file reliably. And without the need for editing, the text format is a wast of time due to the required conversion. Prefer the binary format using fwrite:
fileID = fopen(fullfile(pathname, 'test.txt'), 'w');
% NEVER open a file without checking the success!
if fileID == -1, error('Cannot open file'); end
fwrite(fileID, XYZ, 'double');
fclose(fileID);
If you want to, add the size at the start:
fwrite(fileID, ndims(XYZ), 'uint64');
fwrite(fileID, size(XYZ), 'uint64');
  2 comentarios
Stephen23
Stephen23 el 2 de Nov. de 2016
Editada: Stephen23 el 2 de Nov. de 2016
Josip Strutz's "Answer" moved here:
Hi Jan, thanks for your quick response. I have to make a ascii file, because the program where I process the data only read ascii files. I'am sure that the test.txt file is the right one, because I deleted it before I run the script once again, attached is my file.
Do you have any other solution?
PS: Thanks for the tip with the file open check.
Jan
Jan el 2 de Nov. de 2016
I do not see the problem: I cannot download the data file and the posted text file does not contain any myterious rows. I find exactly what is expected.
Perhaps you want to transpose the data, when you export them by fprintf?
fprintf(fileID,'%.f %.f %.f\n', XYZ.')
Matlab handles array in columnwise order. fprintf processes the input elementwise, put you might expect it to be written row-wise.

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