How to write a empty line (carriage return) to a file during export of string cell array?

Asked by Gene on 8 Aug 2012
Latest activity Answered by Jan Simon on 19 Aug 2012

I read in a text file of multiple lines into a cell array (nx1). I manipulate some of the cells. Some of the line in the text file is just an empty line (carriage return). I then write out the cell array into a text file using the code below.

    for n = 1:length(cc)
         ...
         dlmwrite(file_3, cc{n}, 'delimiter','','-append');
    end

The program work fine except that the empty lines are not writtine in the newly created text fle.

0 Comments

Gene

Products

No products are associated with this question.

1 Answer

Answer by Jan Simon on 19 Aug 2012
for n = 1:length(cc)
   ...
   if isempty(cc{n})
      fprintf(file_3, '\n');
   else
      dlmwrite(file_3, cc{n}, 'delimiter','','-append');
   end
end

I assume, this would be faster:

fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file for writing'); end
fprintf(fid, '%s\n', cc{:});
fclose(fid);

0 Comments

Jan Simon

Contact us