Azzera filtri
Azzera filtri

Adding a string to a specific place in MATLAB

4 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 19 Lug 2012
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.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by