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

3 views (last 30 days)
Hugo
Hugo on 12 Mar 2021
Edited: Jorg Woehl on 12 Mar 2021
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

Accepted Answer

Jorg Woehl
Jorg Woehl on 12 Mar 2021
Edited: Jorg Woehl on 12 Mar 2021
% 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.

More Answers (1)

Hugo
Hugo on 12 Mar 2021
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 Comment
Jorg Woehl
Jorg Woehl on 12 Mar 2021
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.

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by