Extracting number from output file

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

回答 (1 件)

Cedric
Cedric 2013 年 4 月 5 日
編集済み: Cedric 2013 年 4 月 5 日

0 投票

>> 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..

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2013 年 4 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by