I'm using MATLAB to open a batch of CSV files containing column headers and data (using the importdata
function), then I manipulate the data a bit and write the headers and data to new CSV files using the dlmwrite
function. I'm using the -append
and newline
attributes of 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 this blank line was not there before when I read in the data ... and I'm not using newline
on my final call o开发者_如何学运维f dlmwrite
.
Does anyone know how I can keep from writing this blank line to the end of my CSV files?
EDITED 5/18/10 1:35PM CST
Added information about code and text file per request ... you'll notice after performing the procedure below that there appears to be 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 a sample of the code I am 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')
If you don't specify 'newline' option in DLMWRITE, MATLAB uses UNIX-style end-of-line character (LF, \x0A in hexadecimal code), even on Windows machine. But it does puts the newline anyway.
If you specify 'newline','pc', it put DOS end-of-line character (CRLF, \x0D0A in hexadecimal code).
Notepad understands only DOS end-of-line. Wordpad understands both. At least how it is on my machine.
I would recommend you to be consistent and use the same 'newline' option for all the lines. This way you will also avoid the for-loop. Then try to open the output file in MATLAB editor.
Why you don't want the end-of-line character at all? If this is what you need, use FPRINTF function. BUt to do this you have to open the file first with FOPEN. It probably will be better to rewrite the whole code for the output.
Something like this:
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);
精彩评论