remove space line from a text file

15 Ansichten (letzte 30 Tage)
sara
sara am 26 Feb. 2012
Kommentiert: Jan am 30 Okt. 2015
I need to remove a space line from a text file. Any help is appreciated. Sara

Akzeptierte Antwort

Jan
Jan am 26 Feb. 2012
% 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);
  2 Kommentare
Jan
Jan am 26 Feb. 2012
Please explain exactly, what "blanks" mean. I've added a DEBLANK in the code. Does it now do what you need?
Umar
Umar am 13 Feb. 2014
Thanks :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

ALPER AHMED
ALPER AHMED am 28 Okt. 2015
Bearbeitet: ALPER AHMED am 28 Okt. 2015
hi Jan Simon,
how can i delete the second empty line in my text file. (there have 360 text files (angle1,angle2....angle360) and all have the same problem) any help is appreciated.
  1 Kommentar
Jan
Jan am 30 Okt. 2015
Please open a new thread for a new question. Posting a question in the section for answers of another question is confusing.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by