简体   繁体   中英

Editing a Text File from Matlab

I'm still getting used to Matlab, and not sure if this is possible using Matlab or not, but it's just something that popped into my head that I thought could be interesting.

Is there any way to edit the contents of a text file in Matlab?

Moreover, is there any way to edit specific parts of the text file without altering the rest?

To elaborate, let's say I had a text file that was several lines long. For instance:


This is a hypothetical text file.

The cat chased a mouse.

The mouse ran into a hole.

The cat tried to paw at the mouse.

The mouse waited in the hole until the cat got bored.

The mouse came back out when the cat left.


Is there any way to use Matlab to exclusively edit, say, line 6 and change it from "The mouse waited in the hole until the cat got bored" to "The mouse fell asleep and the cat got bored", without having to change the rest of the file?

I know of several methods to read and display contents of text files using Matlab, but I'm not sure if there's any way to actually edit the text files in Matlab.

Thanks!

As far as I know, you will always have to read the file line by line (for instance into a cell-array) and edit it as you need. After that, you write a new file or overwrite the old one.
Of course, you can encapsulate this procedure and then call you own function like

manipulateFile(lineNumber, newLineText)

Some commands that may come in handy are fopen , fscanf , textread , fprintf , and fclose .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM