フィルターのクリア

how to change specific row for all file inside a folder?

3 ビュー (過去 30 日間)
adhi dermawan
adhi dermawan 2022 年 12 月 19 日
コメント済み: adhi dermawan 2022 年 12 月 20 日
Greeting sir, I have multiple text file inside a folder which has 18 rows in each file. I wanted to delete the last row of all file inside the folder. How can I do it?
  1 件のコメント
Rik
Rik 2022 年 12 月 19 日
What have you tried? Do you have a list of file names stored in a variable?

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

採用された回答

Jan
Jan 2022 年 12 月 19 日
編集済み: Jan 2022 年 12 月 19 日
Two steps:
  1. Implement the code to process 1 file. The easies way is to import the file using readlines, remove the last line and to export it by writelines afterwards.
  2. Run the above code in a loop. See FAQ: How can I process a sequence of files?
If you have a large number of huge files, maybe this can help to improve the speed: FEX: FileResize . Then determine the start of the last line:
file = 'YourFile.txt';
[fid, msg] = fopen(file, 'r'); % Not in 't' mode!!!
assert(fid > 0, msg); % No FOPEN without checking for success!!!
s = '';
while ischar(s)
c = ftell(fid); % Get current position
s = fgets(fid); % Read next line
end
fclose(fid);
FileResize(file, c, 'set');
Of course: Create a backup of the data set before testing.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by