Extract number from a specified row in a text file
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
FengwuQiu
el 30 de Mzo. de 2015
Comentada: Michael Haderlein
el 31 de Mzo. de 2015
I have a text file as following; ...... Generating defect surface mesh. Validating triangle mesh. Defect surface time: 0.012 sec Writing analysis results to output file 'myten.15400000.dislocations.ca'. Smoothing defect surface mesh. Calculating normals of defect surface mesh. Wrapping defect surface mesh at simulation cell boundaries. Writing dislocations to output file 'myten.15400000.dislocations.vtk'. Simulation cell volume: 1.38059e+07 Total dislocation line length: 4785.1 [bcc] 1/2<111>: 3729.51 [bcc] 100: 1055.58 [bcc] 110: 0 Other Burgers vectors: 0 Total analysis time: 17.225 sec.
Now, I want to extract the volume, total dislocation line length, and three other line length.That is to say, I want to get these values, 1.38059e+07, 4785.1, 3729.51, and 1055.58
The txt file is attached.
0 comentarios
Respuesta aceptada
Michael Haderlein
el 30 de Mzo. de 2015
Editada: Michael Haderlein
el 30 de Mzo. de 2015
If the file size is this small and you don't have too many files to read, you can go with this very simple solution:
keywords={'Simulation cell volume: ','Total dislocation line length: ',' [bcc] 1/2<111>: ',' [bcc] <100>: ',' [bcc] <110>: '};
output=zeros(size(keywords));
fid=fopen('test.txt');
curline=fgetl(fid);
while ischar(curline)
output(cellfun(@(c) ~isempty(findstr(curline,c)),keywords))=str2double(curline(strfind(curline,':')+1:end));
curline=fgetl(fid);
end
fclose(fid);
In case speed is crucial, the line assign the values in output could be changed to stop Matlab evaluating non-necessary things.
2 comentarios
Michael Haderlein
el 31 de Mzo. de 2015
If this code answers your question, I'd appreciate you marking my answer as "accepted".
Más respuestas (0)
Ver también
Categorías
Más información sobre Other Formats 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!