Reading a complex text file and building a matrix
Mostrar comentarios más antiguos
Hello MATLAB experts,
I am stuck at a typical problem and would appreciate your help a lot. I am trying to read a complex file(attached - - example.txt). This file has millions of lines I truncated it to only 2000.
My aim is simple:
- If a column is '--- detector1 ---'.
- increament 'numberofgammaclusters'.
- Then X = first numerical digit, Y = second numerical digit, and A(X,Y) = third numerical digit.
- read this till next '--- detector1 ---' is encountered. On the this encounter repeat the steps 2 and 3 are repeated.
The sample code that I am trying is below. Please let me know. Any help regarding the improvements in the code or any advice in the approach is hugely appreciated.
A= zeros(256, 256);
E = importdata('gamma.txt', ' ');
numberofgammaclusters=0;
for i=1:1082952
if E.textdata(:,2)==contains('detector1')
numberofgammaclusters=numberofgammaclusters+1;
A()= % The values at second last column % Part of the code I don know how to write
end
end
Thanks very much in advance.
Regards,
Sanchit Sharma
3 comentarios
per isakson
el 30 de Jul. de 2019
There is a number, numberofgammaclusters, of blocks like
--- detector1 ---
PixelHit 153, 88, 2158.3, 0
PixelHit 153, 89, 3490.69, 0
PixelHit 154, 88, 687.456, 0
PixelHit 154, 89, 2675.81, 0
PixelHit 155, 89, 3452.2, 0
PixelHit 156, 90, 3139.74, 0
PixelHit 156, 91, 2414.16, 0
in the file.
Do you want to calculate one A(X,Y) for each block or one A(X,Y) for the entire file? Or am I missing something?
Sanchit Sharma
el 30 de Jul. de 2019
Editada: Sanchit Sharma
el 30 de Jul. de 2019
Sanchit Sharma
el 30 de Jul. de 2019
Respuesta aceptada
Más respuestas (1)
Bob Thompson
el 30 de Jul. de 2019
Editada: Bob Thompson
el 30 de Jul. de 2019
1 voto
I have not been able to utilize your example file, it's a limitation on my end.
That being said, this is how I would look at doing what I understand you're looking for.
fid = fopen('gamma.txt');
line = fgetl(fid);
c = 1;
while isnumeric(line)
if length(line) > 8 & strcmp(line(1:8),'PixelHit')
tmp = regexp(line,' ','split');
A(str2num(tmp{2}),str2num(tmp{3})) = str2num(tmp{4})
end
line = fgetl(fid);
c = c+1;
end
Might need to do some minor editing, because I couldn't use your example file, but the basic concept is sound. If you're looking to capture other data, just add an elseif condition.
This will take some time, but any method (as far as I know) for reading a 2mil line text file is going to take some time.
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



