How to read data from txt between 2 lines with the same symbol?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Artem Smirnov
el 11 de Oct. de 2017
Comentada: Artem Smirnov
el 11 de Oct. de 2017
- 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...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168128/image.png)
0 comentarios
Respuesta aceptada
dpb
el 11 de Oct. de 2017
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!