Extract number from a specified row in a text file
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Michael Haderlein
2015-3-30
编辑:Michael Haderlein
2015-3-30
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 个评论
Michael Haderlein
2015-3-31
If this code answers your question, I'd appreciate you marking my answer as "accepted".
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!