- read the file into memory and operate on it in memory to make the changes desired, then rewrite the modified file in its entirety, or
- as you're doing read a record at a time, make any modifications needed, then write the original/modified record to a new file.
Append and add lines in text/dat file
27 views (last 30 days)
Show older comments
Publius on 28 Jul 2022
Commented: Walter Roberson on 31 Jul 2022
I have a .dat file from CFD postprocessing with variable data regularly listed. I am currently reading through the files fine, but I want to add variable data. I attached an image of the data structure below (this is only an excerpt; the full file is hundreds of thousands of lines, which is the reason that I can't do it manually).
Right now, I am just reading the data. But I will need to add a few things, among them:
- Two new lines after line 14, but before line 15.
- Two numbers apppended to the end of each variable line; for example, adding " 0.000000000E+00 0.000000000E+00" to the end of line 21 (the numbers themselves will be calculated using a routine I've already completed)
- Ignoring lines with two numbers on them.
I have the logic worked out, but I am having trouble a) figuring out how to overwrite the current line/append data to a line itself, and b) formatting numbers the same way: scientific notation with 9 digits (easy), captial E (easy), and a plus/minus with two numbers for the exponent.
I have been using fgetl to read through the data, and the easiest way to do this would be (if appending itself is impossible) to rewrite the line in its entirety by simply setting the current line to a different string. There have been a few posts that I've seen that say this is impossible, but I find this hard to believe. Writing data algorithmically like this seems too easy not to be possible.
Thanks for any help.
dpb on 29 Jul 2022
Edited: dpb on 29 Jul 2022
"..., and the easiest way to do this would be (if appending itself is impossible) to rewrite the line in its entirety by simply setting the current line to a different string. There have been a few posts that I've seen that say this is impossible, but I find this hard to believe."
Well, believe it in general.
Sequential text files are, well, "sequential" -- they're just a string of bytes -- the only way to replace text in line in a text file (that isn't record oriented as is supported by Fortran, but NOT supported by C/MATLAB) is if the new line is exactly the same number of characters as the line it is replacing. Otherwise it will overwirte subsequent characters if longer or leave old data that was part of the previous line if write fewer characters.
The way to do something like this is either
With large files that may be too large for memory, the second is the obvious choice...I've written and posted several such filters on Answers in the past; unfortunately I don't have a link to any of them to point to a particular instance.
Walter Roberson on 31 Jul 2022
MATLAB used to be supported on DEC VAX VMS, which is an operating system that really did support changing lines in the middle of text files. Text files were effectively databases with a sequence number for each record, so you could rewrite a line by changing the record associated with that sequence number, and you could insert lines by using an unused sequence number between the existing records.
However, VMS supported two kinds of files, one as described, and one which was just a stream of bytes. And MATLAB never supported operations on the one described above (it required calls to a different I/O library that was specific to VMS and the older DEC RSX operating system.)
More Answers (0)
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!