How to delete the first line of a text file?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
oblivious
2012-6-5
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!