Extract number from a specified row in a text file

1 view (last 30 days)
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.

Accepted Answer

Michael Haderlein
Michael Haderlein on 30 Mar 2015
Edited: Michael Haderlein on 30 Mar 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 Comments
FengwuQiu
FengwuQiu on 30 Mar 2015
Hi Michael,
Thanks for your constructive solution!
Best regards, Fengwu
Michael Haderlein
Michael Haderlein on 31 Mar 2015
If this code answers your question, I'd appreciate you marking my answer as "accepted".

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!