How do I Read a file line by line after a certain character?
2 次查看(过去 30 天)
显示 更早的评论

This is the text file that I have and I want to read the line after the line "ELEM="
That means, whenever matlab will find "ELEM= " it will jump to the next line and read that line, for example, for the first case, it will read
165 -0.21823E-003
Thanks in Advance.
回答(1 个)
Walter Roberson
2022-4-27
If the file fits into memory, then
filename = 'FileNameGoesHere.ExtensionGoesHere';
S = fileread(filename);
next_lines = regexp(S, '(?<=\^\s*ELEM=[^\n]*\n)[^\n]*', 'lineanchors', 'match');
Note that this will record the entire content of the lines, spaces and all, and will not record the numbers after the ELEM=, and makes no attempt to mark boundaries (which you would need if there are some places where the number of lines after the ELEM= is not exactly the same.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!