read a number after a specific string in a txt file

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

 Accepted Answer

Cedric
Cedric on 29 Mar 2013
Edited: Cedric on 29 Mar 2013
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 Comments

Sorry, there is no relation between the two wanted strings, lets explain the need again:
Food 1
Travel 5
I need the 1 number and the 5
Thanks
Then do as follows:
>> buffer = fileread('myFile.txt') ;
>> str2double(regexpi(buffer, '(?<=Food\s+)\d+', 'match'))
ans =
1
>> str2double(regexpi(buffer, '(?<=Travel\s+)\d+', 'match'))
ans =
5
If you were dealing with floating point numbers, you could replace the
\d+
by
[\d\.]+
Tanks Mr Cedric Wannaz the pb is done but i see that is slowly in a 15 M octes txt file and with a wanted expressions like
Amount of Food is 1
Desired Travel is 5
Please is there any idea to do it very quickly (i need much of reads), Thanks
Your options will be either regular expressions, which are extremely powerful but introduce some overhead, or STRFIND + extra work, which is faster for simple patterns but becomes extremely complicated and slow for more elaborate pattens. I gave you an answer based on regexp this time, because I answered your last question using STRFIND.
Now you can implement both and compare. STRFIND is likely to be faster as the pattern is simple.
>> buffer = fileread('myFile.txt') ;
>> substr = 'Amount of Food is' ;
>> loc = strfind(buffer, substr) ;
>> food = sscanf(buffer(loc+numel(substr):end), '%d', 1)
food =
1
>> substr = 'Desired Travel is' ;
>> loc = strfind(buffer, substr) ;
>> travel = sscanf(buffer(loc+numel(substr):end), '%d', 1)
travel =
5
You can even make this more concise by building an anonymous function..
>> findItem = @(substr) sscanf(buffer(strfind(buffer, ...
substr)+numel(substr):end), '%d', 1) ;
>> findItem('Food is')
ans =
1
>> findItem('Travel is')
ans =
5
Tanks a lot Mr Cedric Wannaz it is super with the anonymous findItem function.
You're most welcome!
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]);

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!