dlmwrite creates an extra empty line - how do i remove it?
Mostra commenti meno recenti
hi all I have a proprietary file which holds a 3X93 integer matrix. I want to save it as a text file using dlmwrite. It loads correctly the file and when i save the data as a text file using dlmwrite it correctly saves the 3X93 elements but also adds a fourth line with an empty space. this empty space messes up the following processing i need to do on this text file. any idea on how to avoid the creation of this extra space or any suggestions for a further step to remove it from the text file?
thanks so much!
Risposte (1)
Walter Roberson
il 3 Nov 2016
0 voti
My suspicion is that what you are noticing is that dlmwrite() ends the last line with standard end-of-line characters (see the 'newline' option for which end-of-line it uses). Often that is the Right Thing To Do, but sometimes it confuses input processing that expects the end of file at the end of the last valid line instead of at the beginning of what would be the next line.
If that is what is happening, then you will need to do the fopen/fprintf/fclose yourself, or you will need to use some process that removes the final end-of-line. Or you will need to fix the processing of the files to take this into account.
There is another possibility which should morally be completely obsolete but is not completely gone yet, which is that the old DOS way of signaling end of file was not by the file headers storing information about how big the file is, but rather by there being an end-of-file character, ascii position 26, control-Z, stored in the file. If present that can look like an extra line if the files are not read in DOS mode. I don't think dlmwrite creates such files, but I have not tested to be sure.
4 Commenti
Michelle Johnson
il 3 Nov 2016
Walter Roberson
il 3 Nov 2016
Untested...
function dlmwrite_without_final_newline(YourFileName, YourMatrix, precision, delimiter, useCRNL)
%write numeric data out in delimited format, but with
%no end-of-line character on the last line
%* YourFileName - name of file
%* YourMatrix - numeric array of data
%* precision - format to write data with, or empty; default is '%.15g'
%* delimiter - delimiter to use between fields, or empty; default is ','
%* useCRNL - non-zero (true) if CR+LF should be used, zero (false) if LF alone should be used, or empty; default on PC is true, on non-PC is false
if ~exist('precision', 'var') || isempty(precision)
precision = '%.15g';
end
if ~exist('delimiter', 'var') || isempty(delimiter)
delimiter = ',';
end
if ~exist('useCRNL', 'var') || isempty(useCRNL)
useCRNL = ispc();
end
if useCRNL
attr = 'wt';
else
attr = 'w';
end
cols = size(YourMatrix,2);
last_line_fmt = [repmat([itemfmt delimiter], 1, cols-1), itemfmt];
line_fmt = [last_line_fmt '\n'];
[fid, message] = fopen(YourFileName, attr);
if fid < 0
error('Failed to open file "%s" because "%s"', YourFileName, message);
end
fprintf(fid, line_fmt, YourMatrix(1:end-1, :).' ); %transpose is important here
fprintf(fid, last_line_fmt, YourMatrix(end,:) );
fclose(fid)
Michelle Johnson
il 4 Nov 2016
Walter Roberson
il 4 Nov 2016
Sorry, that should be precision instead of itemfmt
Categorie
Scopri di più su Text Files in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!