How do I overwrite part of a .tsv file?
7 ビュー (過去 30 日間)
古いコメントを表示
I need to replace some old data in a .tsv file with new data. I need to keep the first 13 rows of the original file and overwrite the following 40000 rows with 40000 rows of the new data. I have used the following code for this:
dlmwrite('MyFile.tsv',M,'precision','%.6f','delimiter','\t','roffset',13);
, which appends my new data on the original file. However, I cannot find a way of overwriting only part of the original file...'-append' does not seem to work/help.
Any help would be appreciated.
0 件のコメント
回答 (2 件)
Image Analyst
2023 年 2 月 21 日
Try this
data = dlmread(filename); % Get old data.
% Now change ONLY rows 14 and lower.
% Now write back the entire matrix.
dlmwrite('MyFile.tsv', M, 'precision', '%.6f', 'delimiter', '\t');
3 件のコメント
Image Analyst
2023 年 2 月 21 日
Personally I'd just use fprintf() to manually write it out line by line.
Walter Roberson
2023 年 2 月 21 日
dlmwrite always overwrites all of the text file unless -append is used. There is no way to get dlmwrite to overwrite part of a text file.
There is also no way to get writetable or writematrix or writecell to overwrite part of a text file.
The fundamental technical implementation of text files, in all operating systems since Vax VMS RMS, has only permitted overwriting with exactly the same number of characters. (There are also operating system calls to truncate a file, but MATLAB does not provide access to those)
2 件のコメント
Walter Roberson
2023 年 2 月 21 日
You can use readcell() and writecell to overwrite the entire file while keeping the identity of the initial lines as text, but there is the danger that the headers might get extra tabs put on the end so that the number of columns matches.
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!