How to find and remove certain text from a text file
24 views (last 30 days)
Show older comments
Hi,
I am having trouble removing specific text from a text file. I have a long text file that contains 5 columns and many rows
I am looking for something similar to the functionality of ctrl+F and then remove all of the strings. Essentially I want to isolate the file so it just displays only positive and negative numbers next to Sum-Pnt
3 Comments
Rik
on 13 Aug 2019
Did you delete comments on this thread? That is considered very rude. You're getting free help with your problem, the least you can do is leaving the thread for other people with a similar problem.
And if your flag ("Similar answers available elsewhere on mathworks") is true, why did you need to post the question? Apparently the other threads weren't enough to help you. Maybe this thread will help someone in the future.
@Bob, can you confirm there are deleted comments? In that case, feel free to flag this question so they can be restored.
Accepted Answer
Bob Thompson
on 8 Aug 2019
As I mentioned, the command would only work for one line at a time. You would need to loop it to get all the lines.
fid = fopen('myfile.txt');
line = fgetl(fid);
c = 1;
while ~isnumeric(line)
number(c) = regexp(text,'Sum-Pnt(.\d.\d+)\s+.*','tokens');
line = fgetl(fid);
c = c + 1;
end
2 Comments
Bob Thompson
on 8 Aug 2019
Does c advance? The purpose of c is to show you which line you should be reading at a given time.
If you are not sure if it is actually reading each line, you can set a debug point somewhere within the loop and compare the string of line with the corresponding line in the file (line c).
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!