MATLAB: How to change text in textfile with matlab

for loopMATLABtext file

Hi!
How can i change .txt file thas has a text like "Adam went to the store", change to "Adam_went_to_the_store" with a for-loop?
Thanks!

Best Answer

Refer to the documentation of fileread & regexp and Export to Text Data Files with Low-Level I/O.
The following code might help you to change a particular text:
Let's assume that the following content "Adam went to the store" is in a text file named textFile.txt
textChar = fileread("textFile.txt");
indices = regexp(textChar,' '); % get indices of the ' ' in textChar
textChar(indices) = '_'
If you want to do the same using for loop, then iterate through textChar (from above) to find ' ' and replace it with '_' .