How to find and remove certain text from a text file

조회 수: 24 (최근 30일)
Pineapple
Pineapple 2019년 8월 8일
편집: Pineapple 2019년 8월 14일
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
Rik
Rik 2019년 8월 13일
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.
Pineapple
Pineapple 2019년 8월 14일
@Rik I apologize. I made edits but I will restore necessary info and I have unflagged. The text file I was referring to was in the form shown below and the loop is in the next answer.
Sum-Pnt .5464 -.650 +.650 .5464 -+-+-+
Sum-Pnt .3244 -.650 +.650 .3244 -+-+-+
Sum-Pnt-.9723 -.650 +.650 -.9723 -+-+-+

댓글을 달려면 로그인하십시오.

채택된 답변

Bob Thompson
Bob Thompson 2019년 8월 8일
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
Bob Thompson
Bob Thompson 2019년 8월 8일
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).
Pineapple
Pineapple 2019년 8월 14일
편집: Pineapple 2019년 8월 14일
The loop worked once "text" was replaced with "line". Thank you for your help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by