Searching for values that a particular variable takes in a single text file

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

What do you mean by at different times? can you provide a short example with expected result?
For example: A variable average= 200, 550, 125, etc. based on the dataset it is working on. But all these values of "average" appear in the same text file.
Please post a copy/past of part of the file (e.g. 10-20 lines) so we can see the format.
This is how the text file looks. I need to check if the AvgValue is greater than or lesser than a particular threshold everytime, the counter encounters the AvgValue.
44 20 4 516
45 20 4 515
43 21 4 554
42 22 4 578
43 22 4 540
41 23 4 576
41 24 4 426
41 25 4 300
47 25 4 302
41 26 4 285
45 26 4 177
46 26 4 136
47 26 4 337
48 26 4 343
41 27 4 349
47 27 4 380
48 27 4 533
49 27 4 92
41 28 4 374
42 28 4 178
43 28 4 156
44 28 4 154
46 28 4 315
47 28 4 478
48 28 4 558
40 29 4 444
44 29 4 266
45 29 4 452
46 29 4 496
AvgValue: 378.000000
ROI: 2
NrOfVoxels: 34
45 37 5 395
46 37 5 331
44 38 5 506
45 38 5 480
46 38 5 412
42 39 5 529
43 39 5 566
45 39 5 554
46 39 5 495
40 40 5 557
43 40 5 587
44 40 5 564
46 40 5 463
47 40 5 416
48 40 5 221
40 41 5 574
46 41 5 452
47 41 5 431
48 41 5 128
43 42 5 525
45 42 5 439
46 42 5 374
42 43 5 532
43 43 5 485
44 43 5 478
45 43 5 367
46 43 5 406
47 43 5 295
43 44 5 503
44 44 5 522
45 44 5 507
46 44 5 564
45 45 5 606
45 46 5 687
AvgValue: 469.000000

Sign in to comment.

 Accepted Answer

If your text file contains only numbers, you can use
M=dlmread('YourFilename.txt')

3 Comments

It contains text too. I submitted a part of the readings in the file.
You can use fgetl to read your file then parse the result with regexp

Sign in to comment.

More Answers (1)

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

Thank you. It's working. The only problem is the huge size of the file. But nothing can possibly be done about it. Thus, its taking almost 20 minutes to print the result. But thanks for the solution.
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.
The file size is around 688 kB. The number of occurrences of AvgValue is 546 times.
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{:}]) ;

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!