How to load large number of files and perform looping through each file?
12 views (last 30 days)
Cedric Wannaz on 4 Oct 2017
You can use the output of DIR to produce a struct array of file names/folders and iterate through files:
D = dir( '*.txt' ) ;
for fileId = 1 : numel( D )
% Do something with D(fileId).name.
and then there are many functions (TEXTSCAN, IMPORTDATA, READTABLE, etc) for reading/parsing the content of text files when the structure seems to be a regular table/array with 3 rows of header.
If you need data from the header, you can use e.g. pattern matching through REGEXP.
Train on a single file, and when you understand how that work try to integrate it in the loop. Here I created a file named based on your example, and I filed it with the slice of content that you provided.
>> content = fileread( 'GSM-2_2002244-2002273_0030_GRGS_0080_03v3.txt' ) ;
>> earthParams = str2double( regexp( content, 'EARTH (\S+) (\S+)', 'tokens', 'once' )) ;
with that you get:
>> data = textscan( content, '%s%d%d%f%f%f%f%f%f%s', 'HeaderLines', 3, 'CollectOutput', true ) ;
and I let you look at what data contains.