Extracting number from output file
Show older comments
Hello,
I'm trying to extract a number from a large text file. The line I'm looking for looks something like this:
SCF Done: E(RB3LYP) = -78.5770732414 A.U. after 9 cycles
How would I extract "-78.5770732414" using fscan or textscan?
Thanks in advance for your help!
-Jim
Answers (1)
>> buffer = fileread('data.txt') ;
>> loc = strfind(buffer, 'SCF Done: E(RB3LYP)') ;
>> value = sscanf(buffer(loc+22:end), '%f')
value =
-78.5771
If you had more work to accomplish on the pattern matching side, you could use REGEXP. Example
>> str2double(regexp(buffer, '(?<=SCF Done: E\(\w{6}\) =\s*)-?[\d\.]*(?=\s*A\.U\.)', 'match'))
ans =
-78.5771
this regexp pattern would match any occurrence of SCF.. where RB3LYP could be any 6 alpha-numerical characters code and where the number would be followed by A.U..
Categories
Find more on Characters and Strings 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!