Searching for values that a particular variable takes in a single text file
Show older comments
Hello,
I need to work on the values that a particular variable takes up at different times in a particular text file. I need these values so that I can compare them with a certain threshold level that I will set in the program. I am a new to MATLAB. Please help.
Thank you.
4 Comments
Azzi Abdelmalek
on 13 Aug 2013
What do you mean by at different times? can you provide a short example with expected result?
Abhishek
on 13 Aug 2013
Cedric
on 13 Aug 2013
Please post a copy/past of part of the file (e.g. 10-20 lines) so we can see the format.
Abhishek
on 13 Aug 2013
Edited: Azzi Abdelmalek
on 13 Aug 2013
Accepted Answer
More Answers (1)
Cedric
on 13 Aug 2013
I would go for something like:
buffer = fileread('data.txt') ;
match = regexp(buffer, '(?<=AvgValue:\s*)[\d\.]+', 'match') ;
avgValue = str2double(match) ;
and then analyze avgValue which is a numeric array.
5 Comments
Abhishek
on 13 Aug 2013
Cedric
on 13 Aug 2013
How large is your file? There are more efficient approaches, but I proposed this one because it solves your problem with 3 lines of code.
Abhishek
on 14 Aug 2013
Cedric
on 15 Aug 2013
If it is really 688kB (and not MB), could you email me the file please so I can perform some tests? This is a small file actually, so its processing shouldn't take more than a few seconds.
Ok, the positive look-behind takes time. Please, try the following:
buffer = fileread('feedback0005_plots_Aphasia.ert') ;
tokens = regexp(buffer, 'AvgValue:\s*([\d\.]+)', 'tokens') ;
avgValue = str2double([tokens{:}]) ;
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!