Extract number from a specified row in a text file
2 views (last 30 days)
Show older comments
FengwuQiu
on 30 Mar 2015
Commented: Michael Haderlein
on 31 Mar 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 Comments
Accepted Answer
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
Michael Haderlein
on 31 Mar 2015
If this code answers your question, I'd appreciate you marking my answer as "accepted".
More Answers (0)
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!