How to delete the first line of a text file?

6 views (last 30 days)
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?

Accepted Answer

oblivious
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

More Answers (0)

Categories

Find more on Data Import and Export 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!