フィルターのクリア

read a number after a specific string in a txt file

6 ビュー (過去 30 日間)
Ferial Assmani
Ferial Assmani 2013 年 3 月 29 日
コメント済み: Oscar Espinosa 2020 年 5 月 30 日
i want to read the number after a specific string in a txt file, for example:
First parameter is 1
Second Parameter is 5
I want like result P1 = 1 and P2 = 5, I note that there is a variable spaces between the wanted strings and the wanted data. Tanks

採用された回答

Cedric
Cedric 2013 年 3 月 29 日
編集済み: Cedric 2013 年 3 月 29 日
As it's a bit more elaborate than your previous question, it might be time to go for a regexp solution (even though you can always use STRFIND, SSCANF, etc).
Are these parameters listed in an increasing order? I.e. could we detect "parameter is" and get what follows iteratively, in order to build an array P whose 1st element is what you call P1, second element is what you call P2, etc?
I'm asking, because you could have a solution like
>> buffer = fileread('theFile.txt') ;
>> P = str2double(regexpi(buffer, '(?<=parameter is\s*)\d*', 'match'))
P =
1 5
If parameters are not ordered, we have to match them more specifically though.
  7 件のコメント
Cedric
Cedric 2013 年 3 月 31 日
You're most welcome!
Oscar Espinosa
Oscar Espinosa 2020 年 5 月 30 日
Hi, I tried this solution, but doesn't fit in my case. I have a line, which need to be read in each iteration due to the change in its value. The lines is the next:
*GET G FROM SSUM ITEM=ITEM G VALUE= 0.238690015E+13
What I have tried its without success:
str2double(regexpi(buffer, '(?<=VALUE= \d*', 'match'));
and
sscanf(buffer, '%*s %15.9e', [1, inf]);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by