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

24 visualizzazioni (ultimi 30 giorni)
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') .';

Risposta accettata

dpb
dpb il 23 Apr 2020
Modificato: dpb il 23 Apr 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 Commenti
dpb
dpb il 23 Apr 2020
Forgot the output to write...
fwrite(fido,buf)
dpb
dpb il 23 Apr 2020
Of course, one can just use
...
fwrite(fido,fread(fidi,'*char')); % suck up the rest as image and spit it out verbatim
...

Accedi per commentare.

Più risposte (2)

Geoff Hayes
Geoff Hayes il 22 Apr 2020
Ivan - perhaps try using importdata and indicate the number of header lines that should be ignored when reading in the file.
  2 Commenti
Ivan Mich
Ivan Mich il 22 Apr 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 il 23 Apr 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.

Accedi per commentare.


dpb
dpb il 22 Apr 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 Commenti
Ivan Mich
Ivan Mich il 22 Apr 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 il 23 Apr 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)

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by