how to identify a string in an external text file and save the text of the two following lines in arrays, by delimiters

조회 수: 2 (최근 30일)
Hello,
I would like to identify a given string in a text file, for example "A C G Y R E" in the text file I attach and save the strings of the following 2 lines in 4 column arrays, each with 2 elements. In my example the next two lines to the string are:
gt5 2d4 wr5 sw4
ay2 564 jy4 a48
Therefore, I would like to create and fill 4 arrays:
array1=[gt5;ay2]
array2=[2d4;564]
array3=[wr5;jy4]
array4=[sw4;a48]
Any help is sincerely appreciated.
Best regards,
Hugo

채택된 답변

Jorg Woehl
Jorg Woehl 2021년 3월 12일
편집: Jorg Woehl 2021년 3월 12일
% open textfile
fid = fopen('myfile.txt');
% read line by line until the pattern is found
pattern = 'A C G Y R E';
tline = fgetl(fid);
while (ischar(tline) && ~contains(tline, pattern))
tline = fgetl(fid);
end
% read the next 8 character vectors, delimited by whitespace or a line break
C = textscan(fid, '%s', 8, 'Delimiter',{' ','\r\n'});
% reshape it into 2-by-4 cell array
C = reshape(C{1}, [2 4]);
% close textfile
fclose(fid);
C{:,1} contains 'gt5' and '2d4', C{:,2} contains 'wr5' and 'sw4', and so on.

추가 답변 (1개)

Hugo
Hugo 2021년 3월 12일
Dear Mr. Jorg Woehl,
Thank you very much for your help. Unfortunately, your code does not work on my end. Do you have any other suggestions?
Best regards,
Hugo
  댓글 수: 1
Jorg Woehl
Jorg Woehl 2021년 3월 12일
편집: Jorg Woehl 2021년 3월 12일
Hi Hugo, what exactly does not work on your end? The code works fine with a sample textfile such as:
Praesent commodo cursus magna,
vel scelerisque nisl consectetur
et. Lorem ipsum dolor sit amet, c
onA C G Y R Esectetur adipiscing
gt5 2d4 wr5 sw4
ay2 564 jy4 a48
Donec sed odio dui. Vestibulum id
ligula porta felis euismod semper.
Save this as myfile.txt and run the script above in the same folder. I constructed the sample textfile according to the details that you had given in your question: string "A C G Y R E" somewhere in a given line (here: line 4), followed by the next two lines to the string gt5 2d4 wr5 sw4 (line 5) and ay2 564 jy4 a48 (line 6).
The script assumes that (a) the search string is present in the file, (b) that the next two lines are also present and (c) contain a total of eight words with spaces or line breaks in between. No error checking is performed in the script.
Without knowing what your textfile actually looks like (you haven't attached it to your question) or if its makeup is different from the details you have provided, it is difficult to make any suggestions.

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by