How do I overwrite part of a .tsv file?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Image Analyst
el 21 de Feb. de 2023
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 comentarios
Image Analyst
el 21 de Feb. de 2023
Personally I'd just use fprintf() to manually write it out line by line.
Walter Roberson
el 21 de Feb. de 2023
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 comentarios
Walter Roberson
el 21 de Feb. de 2023
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.
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!