Effacer les filtres
Effacer les filtres

Adding a string to a specific place in MATLAB

4 vues (au cours des 30 derniers jours)
Daniel Wells
Daniel Wells le 19 Juil 2012
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

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Juil 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.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by