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
0 件のコメント
採用された回答
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.
0 件のコメント
その他の回答 (1 件)
Hugo
2021 年 3 月 12 日
1 件のコメント
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 Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!