Adding a string to a specific place in MATLAB

4 次查看(过去 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

采纳的回答

Walter Roberson
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 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by