How to delete the first line of a text file?
4 views (last 30 days)
Show older comments
I'd like to import a text file, delete the first line, and save the remaining lines to a new text file. The first line consists of a single number, while the remaining lines have the same number of elements in each of them. For instance, one of these input text files might look like this:
20
1 2 3 4
5 6 7 8
9 0 1 2
What I'd like to do is take the above text file and create a new text file that looks like this.
1 2 3 4
5 6 7 8
9 0 1 2
I'd also like to name the new text file something that includes the value in the first line (e.g, "filename20.txt"). Help?
0 Comments
Accepted Answer
oblivious
on 5 Jun 2012
I could not put the file name as you wanted. but i did the rest of the things
clear all;
fclose('all')
fid=fopen('oldfile.m','rt');
fid2=fopen('newfile.m','wt');
id=0;
a=fgets(fid);
while(ischar(a))
id=id+1;
if id==1
a=fgets(fid);
continue
else
fprintf(fid2,a);
end
a=fgets(fid);
end
newfile.m will have your desired output
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!