How to read data from txt between 2 lines with the same symbol?

2 次查看(过去 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...

采纳的回答

dpb
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by