MATLAB Answers

Changing a specific column in txt file

8 views (last 30 days)
Varghese
Varghese on 15 Jan 2014
Answered: Walter Roberson on 15 Jan 2014
Dear group, I am looking to change the value in a specific column in a txt file. In the attached txt file, I want to change the 1's in the third column to the previous value (same column, previous row) and delete the previous row. Any help would be much appreciated.

Answers (1)

Walter Roberson
Walter Roberson on 15 Jan 2014
There is no mechanism to change text files "in place" -- not unless the replacement is exactly the same size as the original.
You need to read the file and write each line in turn to a new file, except writing the new versions of the lines instead of writing the old versions. Then once past the part to change, keep reading from the old and copying to the new. Close the files. If needed, rename the new file to have the name of the old file.

Community Treasure Hunt

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

Start Hunting!

Translated by