MATLAB: Read files in a directory

multiple files in a directory

I have to read data files in a directory. The asc files that I need to read are in subfolders. In each subfolder there are four asc files. I only need to access one file in each folder and read the headers and columns below (look at the pictures attached).There are more than 10,000 fies in subfolders that I need to read in. I want to create variables for latitude, longitude,starting-time and the colums CO,z,T (see the attached files). At the end there will be variables such as this.
latitude: 10,000*1
longitude:10,000*1
etc.
z: 10,000*23 (23 is for 23 altitude levels)
T: 10,000*23
CO : 10,000*23
Any help will be greatly appreciated.

Best Answer

This will read the data from one file (attached):
[fid,msg] = fopen('sr36161v4.0.txt','rt');
assert(fid>=3,msg)
% Preamble data:
pfx = struct();
boo = true;
while boo
str = strtrim(fgetl(fid));
boo = ~isempty(str);
if boo
tmp = strtrim(regexp(str,'\|','split'));
val = str2double(tmp{2});
if isnan(val)
pfx.(tmp{1}) = tmp{2};
else
pfx.(tmp{1}) = val;
end
end
end
% Header data:
while isempty(str)
val = ftell(fid);
str = strtrim(fgetl(fid));
end
str = regexprep(str,{'\s*\(','\)'},{'_',''});
hdr = regexp(str,'\s+','split');
% Column data:
fseek(fid,val,'bof');
fmt = repmat('%f',1,numel(hdr));
tmp = textscan(fid,fmt,'HeaderLines',1);
tmp = [hdr;tmp];
out = struct(tmp{:});
fclose(fid);
You can access the preample data in pfx, e.g.:
>> pfx.name
ans = ace.sr36161
>> pfx.latitude
ans = 18.430
And the column data in out, e.g.:
>> out.z
ans =
0.50000
1.50000
2.50000
3.50000
4.50000
5.50000
6.50000
7.50000
8.50000
9.50000
.... lots of lines here
143.50000
144.50000
145.50000
146.50000
147.50000
148.50000
149.50000