フィルターのクリア

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 Center および File ExchangeLow-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