If you are not using MS Windows with Excel installed, then you have to use the standard mechanisms to replace a portion of a file:
Open the input file. Open a (different) output file. Read lines from the input file and write them to the output file until you reach a line that needs to be changed. Read the old version of the line from the input file, replace portions of the content with the desired new data, and write the result to the output file. Keep doing that until you are finished changing lines. Then go back to reading input lines and copying them to the output file. At the end, close both files, rename the input file to a backup name, rename the output file to the input file name. If you are really confident in the change than then you can delete the renamed input file. At every step of this process you need to ensure that if you interrupted or the system rebooted or had a glitch or that there was a bug in your code, that you always have a known-good version of the file available to go back to.
This process of copying from input file to output file is strictly necessary for text files in which any output text might not be exactly the same size as the input text. This is a fundamental restriction due to how text files are implemented on pretty much all operating systems since the early 1980's. If the replacement is exactly the same size as the original, then whether the file is binary or not, there is operating system support, but using the two file process is a good idea.
With the facilities provided by MATLAB, this procedure is also necessary if the output file will be shorter at all than the input file (e.g., if lines got deleted); that part is an inexplicable restriction in MATLAB as all operating systems since the 1960's have provided mechanisms to truncate files.