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

25 次查看(过去 30 天)
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') .';

采纳的回答

dpb
dpb 2020-4-23
编辑:dpb 2020-4-23
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 个评论
dpb
dpb 2020-4-23
Of course, one can just use
...
fwrite(fido,fread(fidi,'*char')); % suck up the rest as image and spit it out verbatim
...

请先登录,再进行评论。

更多回答(2 个)

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

请先登录,再进行评论。


dpb
dpb 2020-4-22
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 个评论
Ivan Mich
Ivan Mich 2020-4-22
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 2020-4-23
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by