Stop writing an empty line at the end of the CSV file (using MATLAB)
I use MATLAB to open a batch of CSV files containing column headers and data (using a function importdata
), then I manipulate the data a bit and write the headers and data to new CSV files using the dlmwrite
function. I am using attributes -append
and newline
dlmwrite
to add each line of text / data on a new line.
Each of my new CSV files has a blank line at the end, whereas that blank line wasn't there before when I read the data ... and I don't use it newline
in my last call dlmwrite
.
Does anyone know how I can avoid writing this blank line until the end of my CSV files?
EDITED 5/18/10 1:35 PM CST
Added code and text file information for each request ... you will notice that after following the procedure below, there is a carriage return at the end of the last line in the new text file.
Consider a text file named 'textfile.txt' that looks like this:
Column1, Column2, Column3, Column4, Column 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5
Here's some sample code I'm using:
% import data
importedData = importdata('textfile.txt');
% manipulate data
importedData.data(:,1) = 100;
% store column headers into single comma-delimited
% character array (for easy writing later)
columnHeaders = importedData.textdata{1};
for counter = 2:size(importedData.textdata,2)
columnHeaders = horzcat(columnHeaders,',',importedData.textdata{counter});
end
% write column headers to new file
dlmwrite('textfile_updated.txt',columnHeaders,'Delimiter','','newline','pc')
% append all but last line of data to new file
for dataCounter = 1:(size(importedData.data,2)-1)
dlmwrite('textfile_updated.txt',importedData.data(dataCounter,:),'Delimiter',',','newline','pc','-append')
end
% append last line of data to new file, not
% creating new line at end
dlmwrite('textfile_updated.txt',importedData.data(end,:),'Delimiter',',','-append')
a source to share
If you do not specify the newline option in DLMWRITE, MATLAB uses a UNIX-style end-of-line character (LF, \ x0A in hex) even on a Windows machine. But that still wraps the newline.
If you specify 'newline', 'pc' it will place the end of the DOS line (CRLF, \ x0D0A in hex).
Notepad only understands the DOS endpoint. Wordpad understands both. At least that's how it is on my machine.
I would recommend that you be consistent and use the same newline option for all lines. This way you also avoid the for loop. Then try opening the output file in the MATLAB editor.
Why don't you want a line end character at all? If that's what you want, use FPRINTF . To do this, you must first open the file with FOPEN. It would probably be better to rewrite all the code for output.
Something like that:
fid = fopen('textfile_updated.txt','w');
fprintf(fid,'%s\r\n',columnHeaders); %# \r\n for PC end-of-line
fprintf(fid,[repmat('%d,',1,size(importedData.data,2)-1) '%d\r\n'],importedData.data(1:end-1,:)');
fprintf(fid,[repmat('%d,',1,size(importedData.data,2)-1) '%d'],importedData.data(end,:));
fclose(fid);
a source to share