fprintf add mysterious rows, dlmwrite works
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Jan
am 2 Nov. 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.
Antworten (1)
Jan
am 2 Nov. 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 Kommentare
Stephen23
am 2 Nov. 2016
Bearbeitet: Stephen23
am 2 Nov. 2016
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
am 2 Nov. 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.
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!