MATLAB: Remove space line from a text file

text file

I need to remove a space line from a text file. Any help is appreciated. Sara

Best Answer

% Read the file as cell string line by line:
fid = fopen(FileName, 'r');
if fid < 0, error('Cannot open file: %s', FileName); end
Data = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
fclose(fid);
% Remove empty lines:
C = deblank(Data{1}); % [EDITED]: deblank added
C(cellfun('isempty', C)) = [];
% Write the cell string:
fid = fopen(FileName, 'w');
if fid < 0, error('Cannot open file: %s', FileName); end
fprintf(fid, '%s\n', C{:});
fclose(fid);