How to read data from txt between 2 lines with the same symbol?
1 回表示 (過去 30 日間)
古いコメントを表示
- Hello!
- I have a lot of .txt files (<1000 lines each). The data format is the following (the picture): there are some lines in the beginning that I don't need, then the line with '*', then the lines with data that I need to extract from the file, then again a line with '*' and some comments that I don't need.
- Is there any way to do that? I have a lot of such files. The matter is that in every file the number of lines before the first '*' is different. So, is there any way to read the data in between of two '*'? I tried all the functions but I am a beginner and just cannot come up with the right idea...
0 件のコメント
採用された回答
dpb
2017 年 10 月 11 日
That's pretty simple to do, actually...
fid=fopen('yourfile.txt'); % open the file
while ~feof(fid) % begin loop, we'll break later...
l=fgetl(fid); % read a record into character variable
if strfind(l,'*'), break, end % found the first '*' so quit...
end
data=cell2mat(textscan(fid,repmat('%f',1,4),'collectoutput',1));
The last will read records of four numeric values until it runs out of data or there's a conversion error (which there will be at the next '*' record. But, that's ok, that's all you wanted... :)
To do multiple files, you dir with a suitable filename wildcard pattern to return the list of files and iterate over it.
その他の回答 (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!