필터 지우기
필터 지우기

Adding a string to a specific place in MATLAB

조회 수: 4 (최근 30일)
Daniel Wells
Daniel Wells 2012년 7월 19일
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