Fastest way to create arrays programmatically for particular parameter values from a set of text files
2 views (last 30 days)
Show older comments
I have a collection of folders at location say 'folderpath'. Inside , each of this, I have a txt file myfile.txt. There are 4 parameters in this file "type 0, val1" ,"type 0, val2" , "type 1, val1" and "type 2, val2" .The formatting is exactly as indicated. Note that there are multiple instances of these 4 parameters inside the text file.
My aim is to create 4 arrays , one for each parameter. There are around 200,000 instances of each parameter in the txt file. So I need four arrays of 20,000 size. And I have around 100 folders each of which has such a txt file.
What is the fastest way of achieving this in MATLAB.
I used
content = fileread( 'myfile.txt' ) ;
numbers = str2double(regexp(content, '(?<=Rank 1,val1[^0-9]*)[0-9]*\.?[0-9]+', 'match'))
But it is taking too long.
I am attaching a sample txt file.
0 Comments
Answers (1)
Walter Roberson
on 9 Jul 2019
The fastest way of grepping is MATLAB is to system() out to a Unix grep or egrep utility.
The second fastest way is to invoke perl from MATLAB to do the matching for you.
2 Comments
Walter Roberson
on 9 Jul 2019
The method in MATLAB that is the most efficient for elapsed time and CPU time, is to system() out to a grep or egrep utility. The method that is the most efficient for time spent writing the code is to use the code that you already have, since you already have it.
You have repeatedly emphasized the fastest way, and these are the fastest ways. egrep executables are much more efficient than MATLAB is.
If you wish to confine your implementation to MATLAB without external utilities then Yes, there are ways that are probably more efficient than what you are doing already, but they would not be the fastest that you could do using MATLAB without such a restriction.
See Also
Categories
Find more on File Operations 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!