How to delete the first 2 rows (Headers) of a txt file ?

23 vues (au cours des 30 derniers jours)
Ivan Mich
Ivan Mich le 22 Avr 2020
Modifié(e) : dpb le 23 Avr 2020
Hello,
How to delete the first two rows , which are Headers , in a .txt file? I use these command to read it
file=regexp(fileread('data.txt'), '\r?\n', 'split') .';

Réponse acceptée

dpb
dpb le 23 Avr 2020
Modifié(e) : dpb le 23 Avr 2020
Well, once you've got the right content in memory, then just rewrite the file. But, specifically on that has some bearing on the content for easy ways.
But, if that is the only thing you care to do, then the simplest irrespective of what the file content is, is probably
fidi=fopen('oldfile.txt','r');
fido=fopen('newfile.txt','w');
headerlines=2;
for i=1:headerlines,fgetl(fidi);end % skip the desired number
buf=fread(fidi,'*char'); % suck up the rest as image
fwrite(fido,buf) % and spit it out verbatim
fclose('all') % done
%delete('oldfile.txt') % optionally remove the original keeping new
%movefile('newfile.txt','oldfile.txt') % optionally rename new to old (only new left as oldfile name)
  3 commentaires
dpb
dpb le 23 Avr 2020
Forgot the output to write...
fwrite(fido,buf)
dpb
dpb le 23 Avr 2020
Of course, one can just use
...
fwrite(fido,fread(fidi,'*char')); % suck up the rest as image and spit it out verbatim
...

Connectez-vous pour commenter.

Plus de réponses (2)

Geoff Hayes
Geoff Hayes le 22 Avr 2020
Ivan - perhaps try using importdata and indicate the number of header lines that should be ignored when reading in the file.
  2 commentaires
Ivan Mich
Ivan Mich le 22 Avr 2020
ok thank you, but the point is that I do not want only to read the file. I would like also to create a new .txt file with the content of "data.txt" and without headers. How can I use fopen to do this?
Geoff Hayes
Geoff Hayes le 23 Avr 2020
Ivan - do you have to use fopen or can you use writematrix on your input? Assuming that your text file contains numeric data only then
filename = 'data.txt';
delimeter = ','; % <---- assumes comma separated columns
A = importdata(filename, delimeter, 2);
writematrix(A, filename, 'Delimiter', delimeter);
The above assumes comma separated columns. If this isn't the case, then you can change it to whatever character separates the columns. If there is no delimeter, then this method probably won't work.

Connectez-vous pour commenter.


dpb
dpb le 22 Avr 2020
Or, readtable.
But, starting with the above simply
file=regexp(fileread('data.txt'), '\r?\n', 'split') .';
file=file(3:end);
Not knowing content of the file, or the next steps, hard to know what's the best approach but one would think unless it is just a list of string data in one column that it would be far better to interpret the file on input than just read characters.
  2 commentaires
Ivan Mich
Ivan Mich le 22 Avr 2020
dpb , ok thank you, nut the point is that I do not want only to read the file. I would like also to create a new .txt file with the content of "data.txt" and without headers. How can I use fopen to do this?
dpb
dpb le 23 Avr 2020
Well, once you've got the right content in memory, then just rewrite the file.
If that is the only thing you care to do, then the simplest irrespective of what the file content is, is probably
fidi=fopen('oldfile.txt','r');
fido=fopen('newfile.txt','w');
headerlines=2;
for i=1:headerlines,fgetl(fidi);end % skip the desired number
buf=fread(fidi,'*char');
fwrite(fido)
fclose(all)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by