remove space line from a text file

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

 採用された回答

Jan
Jan 2012 年 2 月 26 日

6 投票

% 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 件のコメント

Jan
Jan 2012 年 2 月 26 日
Please explain exactly, what "blanks" mean. I've added a DEBLANK in the code. Does it now do what you need?
Umar
Umar 2014 年 2 月 13 日
Thanks :)

サインインしてコメントする。

その他の回答 (1 件)

ALPER AHMED
ALPER AHMED 2015 年 10 月 28 日
編集済み: ALPER AHMED 2015 年 10 月 28 日

0 投票

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 件のコメント

Jan
Jan 2015 年 10 月 30 日
Please open a new thread for a new question. Posting a question in the section for answers of another question is confusing.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2012 年 2 月 26 日

コメント済み:

Jan
2015 年 10 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by