Adding a string to a specific place in MATLAB
3 次查看(过去 30 天)
显示 更早的评论
How would one add a line to a specific place in a file? For example, if in the file "test.m" I wanted to insert the line of code " i = 2*x" between lines 37 and 38, how would one do so?
Thanks,
Danny
0 个评论
采纳的回答
Walter Roberson
2012-7-19
Files (text or binary) do not support "insert" or "delete" operations.
It is recommended to instead open the file for reading, open a different file for writing, and to copy everything from the original until you get to the point of changes, make the changes in the output file, and then copy everything appropriate remaining in the input file. Close both files. If necessary, rename the output file to the name of the input file afterwards.
The same recommendation applies to making modifications of exactly the same length in text files: although it is technically possible, few people get it right.
In-place modification of binary files is common but requires experience.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!