필터 지우기
필터 지우기

How to extract text between two specific words using fread operation

조회 수: 3 (최근 30일)
Kanchibhotla Chandra Sekhar
Kanchibhotla Chandra Sekhar 2014년 8월 11일
답변: Walter Roberson 2016년 11월 29일
I have an data file where i need to extract the text between two specific words
<Edata
Line1
Line2
Edata>
I need to Extract the text content between two specific words e.g. "<Edata" and "Edata>" using fread operation. This file will be in unknown extension file (*.kcs), so i can read only by using file operations. Any Idea..?

답변 (2개)

Image Analyst
Image Analyst 2014년 8월 11일
Read the whole thing in using fread, which I assume you know how to do. Then
word1Location = strfind(theString, '<Edata');
word2Location = strfind(theString, 'Edata>');
inBetweenText = theString(word1Location+6:word2Location-1);
  댓글 수: 6
Kanchibhotla Chandra Sekhar
Kanchibhotla Chandra Sekhar 2014년 8월 12일
Actually i am not trying on original data files. I have taken test data files and trying this code. Sorry for that, Edata or FBD are same as i am trying on two different data files. I have not tried on loop, i know that why i not unable to get the entire array. But i tried with line by line code which doesn't work anyway.
while ischar(tline)
/* Code */
tline = fgets(tus8);
end
Have any idea how to do section by section. I have not tried that...
Shirisha Acha
Shirisha Acha 2016년 11월 29일
I was looking for the same answer as to how to loop it. Did you solve it yet? Can u plz share the code if so

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


Walter Roberson
Walter Roberson 2016년 11월 29일
theString = fileread(OldfilePath);
parts = regexp(theString, '(?<=<Edata\s*\n).*?(?=\nEdata>)', 'match');
I took the liberty here of removing the end-of-line character after and immediately before Edata . The meaning of "between" was not well defined here: what should happen if there is other text on the same line, like
<Edata %first one
line 1
line 2
Edata>
then should the '%first one' be extracted?

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by