필터 지우기
필터 지우기

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

조회 수: 25 (최근 30일)
Ivan Mich
Ivan Mich 2020년 4월 22일
편집: dpb 2020년 4월 23일
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일
Forgot the output to write...
fwrite(fido,buf)
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