How do I overwrite part of a .tsv file?
3 次查看(过去 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 个评论
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!