How to delete the first line of a text file?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
oblivious
il 5 Giu 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!