MATLAB: How to delete header and footer from dat. file

data processing

Hi!
I have 5000 dat. file. Each file have 3 header and 3 footer. I need to delete header and footer from each file and then save it again as dat. file.
Can anybody kindly tell me what is the procedure.
Regards
Mainul

Best Answer

Hi,
i guess you need more or less this kind of code :
for i = 1:5000
FileToRewrite = ['DatFile_' num2str(i) '.dat'];
% phase 1 : read the ith dat file
fid = fopen(FileToRewrite,'r');
TextDat = textscan(fid,'%s','delimiter','\n');
fclose(fid);
% phase 2 : just take from the 4th line (3 headers) until the end-3
% (3 footers)
NewTextDat = TextDat{1}(3+1:end-3);
% phase 3 : rewrite the file with the new data
fid = fopen(FileToRewrite,'wt');
fprintf(fid,'%s\n',NewTextDat{:});
fclose(fid);
end
Big Warning : once you run this code, all you .dat files will be rewritten. think to make copies of your originals, in case there is a bug.