Increase read time of large text file
Afficher commentaires plus anciens
I have a text file that is very large, and each line basically looks like this:
A1,1= 2.5 A1,2=1.8e-5 A1,3=4
A2,1= 1 A2,2=3 A2,3=6.4
etc...
I want to extract only the numerical data for each coordinate into a matrix, and I have code to successfully do it. But it takes too long. Here is my current technique:
Str=regexp(chunk{k},'=\s*(\S+)','tokens');
num_data=cellfun(@str2double,[Str{:}]);
I'm reading in 1000 lines at a time into the 'chunk' array. So the chunk{k} just returns a particular line of the file.
Str will contain just the numerical data from the line, but in char format. num_data is the final output for each line in double format.
For an example file that is 20MB, this technique takes 40 seconds. But if I remove this line:
num_data=cellfun(@str2double,[Str{:}]);
then the code only takes 13 seconds.
Any suggestions on how to speed up the conversion from cell array of strings to double array would be greatly appreciated.
Thanks,
Adam
Réponse acceptée
Plus de réponses (1)
Adam
le 25 Sep 2013
0 votes
Catégories
En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!