how to remove the specific line in txt file

7 views (last 30 days)
Seong Hwang
Seong Hwang on 1 Oct 2015
Edited: Guillaume on 1 Oct 2015
the file is below. I want to remove the below lines (line number 1~38) and then the line 39th is becoming the first line. I can load the txt file using the function 'fopen' but I can't think how to modify the file.
# OOMMF OVF 2.0
#
# Segment count: 1
#
# Begin: Segment
# Begin: Header
#
# Title: Oxs_Demag::Field
# Desc: Oxs vector field output
# Desc: MIF source file: D:/oommf/Triangle_m0.mif
# Desc: Iteration: 5100, State id: 44051
# Desc: Stage: 0, Stage iteration: 5100
# Desc: Stage simulation time: -1 s
# Desc: Total simulation time: -2 s
# meshunit: m
# meshtype: rectangular
# xbase: 2.5000000000000001e-009
# ybase: 2.5000000000000001e-009
# zbase: 2.5000000000000001e-009
# xnodes: 220
# ynodes: 220
# znodes: 4
# xstepsize: 5.0000000000000001e-009
# ystepsize: 5.0000000000000001e-009
# zstepsize: 5.0000000000000001e-009
# xmin: 0
# ymin: 0
# zmin: 0
# xmax: 1.1000000000000001e-006
# ymax: 1.1000000000000001e-006
# zmax: 2e-008
# valuedim: 3
# valuelabels: Field_x Field_y Field_z
# valueunits: A/m A/m A/m
#
# End: Header
#
# Begin: Data Text <- 38th line
3.941615e+003 2.196048e+003 1.309809e+002 <-39th line

Answers (2)

Walter Roberson
Walter Roberson on 1 Oct 2015
Often you do not need to do this; if you were using textscan() to read the file you could just specify 'HeaderLines', 38 to cause those lines to be skipped.
Deleting lines "in place" is risky and effectively cannot be done with MATLAB (MATLAB does not have the necessary ftrunc() call to make it work.) So what you need to do is copy what you do want to save into a new file.
fin = fopen('YourFile.txt', 'rt');
for K = 1 : 38; fgetl(fin); end
fout = fopen('NewFile.txt', 'wt');
while true
input_line = fgetl(fin);
if ~ischar(input_line); break; end %end of file
fprintf(fout, '%s\n', input_line');
end
fclose(fout);
fclose(fin);
  1 Comment
Guillaume
Guillaume on 1 Oct 2015
Note that ftrunc() / ftruncate() can only crop the end of the file. Granted you could rewrite the file in place without the header and then truncate.

Sign in to comment.


Guillaume
Guillaume on 1 Oct 2015
Edited: Guillaume on 1 Oct 2015
It is not possible to truncate the beginning of files. Note that this is not a limitation of matlab but the way files work. Your only option is to read the entire file and to rewrite it without the first 38 lines.
To avoid data loss (in case of power failure, etc.) the best procedure is to
  1. read the file skipping over the first 38 lines
  2. write the rest of the file to a temporary file
  3. delete the old file
  4. rename the temporary file to the new file
filename = 'C:\somewhere\somefile.txt';
try
readid = fopen(filename, 'rt');
tempfile = fullfile(tempdir(), tempname()); %note that tempdir and tempname are built-in functions of matlab
writeid = fopen(tempfile, 'wt');
for line = 1:38
fgetl(readid); %read and discard content of the first 38 lines
end
fwrite(writeid, fread(readid)); %and copy rest of file to temp file
fclose(readid);
fclose(writeid);
delete(filename);
movefile(tempfile, filename);
catch mex
%do whatever you want
end
Note that there is no guarantee that file permissions will be preserved that way. If you want that, it's significantly more complicated.

Community Treasure Hunt

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

Start Hunting!