Write m-file that writes a specific code section into a specific location of another m-file

Hello!
I want to create an m-file that rides into another m-file.
Ideally the m-file would replace a specific region (e.g. defined by line numbers) of the second m-file and write a new code section into that instead.
Is there any practicable way to do that?
Thank you!

Answers (1)

readlines(), replace string entries, writelines() preferably to a different file name.
It is common for people to ask to replace text in a file without writing a new file. The way that files are stored in all modern file systems only permits replacement if partial content if the replacement is exactly the same length as what is being replaced (except in the case of replacement at the end where it would be acceptable to extend the file)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 16 Apr 2023

Answered:

on 16 Apr 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!