Trying to delete lines that start with an alphabet on a text file

10 ビュー (過去 30 日間)
mt
mt 2014 年 10 月 20 日
編集済み: Guillaume 2014 年 10 月 20 日
Hi folks,
I have a huge textfile right here with around 100k lines and I am trying to delete all the lines that start with an alphabet (not deleting the ones that start with numbers). Can anybody help? I am having trouble structuring the m file.
Thanks.

採用された回答

Guillaume
Guillaume 2014 年 10 月 20 日
編集済み: Guillaume 2014 年 10 月 20 日
Use fopen, fgetl, fclose to read your file into a cell array and discard the lines you don't want. Or if you want to write a new file, use fopen, fprintf, fclose and write as your read:
fidi = fopen('somefile.txt', 'rt');
fido = fopen('newfile.txt', 'wt');
tline = fgetl(fidi);
while ischar(tline)
if ~isempty(tline) && tline(1) >= '0' && tline(1) <= '9'
%line to keep
fprintf(fido, '%s\n', tline);
end
tline = fgetl(fidi);
end
fclose(fidi);
fclose(fido);
edit: forgot to read the next line!
  2 件のコメント
mt
mt 2014 年 10 月 20 日
I tried it with a smaller text file, but 'Run' never ends for this code. What could be wrong?
Guillaume
Guillaume 2014 年 10 月 20 日
Yep, I forgot a
tline = fgetl(tline);
within the loop. (You could have found that out in the documentation of fgetl that I linked).
I've edited my answer.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by