Info

This question is closed. Reopen it to edit or answer.

How do I remove the first 3 lines from all of the .txt files in a directory?

4 views (last 30 days)
Hello everyone
I am trying to preprocess a large batch of text files in Matlab. To do this, I have to remove the first couple of lines from each of the files. Can anyone share me some code on how to do this? Thanks
  3 Comments
Zubier  Abdullah
Zubier Abdullah on 4 Aug 2017
this is the code i am using so far
files = dir('*.TXT') N = numel(files) count = 0;
for i = 1:N fid1 = files(i).name disp(fid1) a = fopen(fid1,'r') file_name = [sprintf('%1.0f',i) '.txt']; fopen(file_name,'w+');
for K = 1:4
inline = fgetl(a)
end
while ischar(inline)
inline = fgetl(a)
fprintf(file_name,'%s\n\r',inline)
end
fclose('all')
end
so far it is only creating blank files and not replacing the content of the files as i wanted. I have a smaller program where it replaces the text from one file to another but i have already defined the names.

Answers (1)

ES
ES on 1 Aug 2017
You can start here ... https://in.mathworks.com/matlabcentral/fileexchange/42877-find-and-replace-in-files

This question is closed.

Community Treasure Hunt

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

Start Hunting!