It is possible to open a file in a way that permits a mix of reading and writing. You need to be careful with the permissions to ensure that you can write at the current position rather than having all writes automatically go to the end of file (which changes the idea of where the current input position is!), but it is possible to do.
However, on all file systems that MATLAB is supported on, it is not possible to insert or remove text in the middle of a text file and have the file automatically adjust. That kind of work requires line-oriented file systems such as DEC's RSX-11 and VMS "Record Management Services" (RMS) https://en.wikipedia.org/wiki/Record_Management_Services, or fixed-width records such as were common on IBM's MVS. MATLAB was, at one time, implemented on DEC VMS, but it was never implemented in terms of RMS calls so it never supported this facility. I thought I had read that MATLAB was, at one time, implemented on IBM MVS, but at present I cannot find any evidence of that.
Anyhow, this is not an limitation of MATLAB as such: it is a limitation of how files are implemented by operating systems, as streams of bytes, rather than as "lines".
It is possible to replace text within a line with exactly the same amount of text. Replacing an end-of-line in a text file has results that are formally undefined by people who write international standards: it is permitted in the standards for an end-of-line be written as one character fewer or one character more than what was read in. When multibyte character is in effect in the text file, that could be up to 4 bytes difference.
If you wish to replace text with more text than is already present, then in text files, the only way is algorithms that have the effect of reading to end of file and writing it out again "delayed" by the number of inserted bytes.
If you wish to replace text with less text than is already present, then although you could read ahead and move bytes "forward" relative to where they were originally in the file, when you get to the end of where you want to write, there are still more bytes in the file. International standards define an operation to permit files to be truncated (discard everything after a certain point), but MATLAB does not supply any interface for that operation. Therefore in MATLAB, if you want to make a file shorter than it currently is, you have to resort to opening a new output file and copying through what you want to see there, closing files, and moving the new file in place of the existing file.
So... it is a lot easier to just not try to update a text file, and to instead create a new text file with the content you want.
Your existing code has a commented out fclose() / fopen() with 'w', followed by writing out the modified content that you read in. That works, but has a couple of considerations:
- you are on MS Windows and are opening a text file for 'w', which is binary form. Each \n you write will be output as a linefeed only, with no carriage return. That works fine for most newer programs, but it is a problem for old programs such as Notepad, which still expects to see carriage returns. If there is any possibility that the user might expect to use an old program with the file, then use 'wt' instead of 'w' so that carriage returns are also output each time a newline is seen in the output. (Do not do this if you are writing any binary content!)
- If something goes wrong in the writing process, such as disk full or Blue Screen of Death, then you have lost the original file, because opening for 'w' discards the content. You had the new content in memory, but you cannot guarantee that you will be able to write the new content to the file. Therefore unless you have a backup copy of the file, it is safer to write to a different file that you can then substitute for the original file. International standards define operating system "move file" operations as "atomic" (guarantee that even if there is a power failure, the file will either be intact in the old location or in the new location.)