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.

2 Comments

@Anton Fernando: please upload a sample file. We cannot test code on screenshots.
See the attached file.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 18 Dec 2018
Edited: Stephen23 on 18 Dec 2018
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

2 Comments

Anton Fernando's "Answer" moved here:
Wow. Works like a charm. Thank you very much. :)
Stephen23
Stephen23 on 18 Dec 2018
Edited: Stephen23 on 18 Dec 2018
@Anton Fernando: then please show your thanks by accepting my answer.
You made the mistake of writing your comment in an "Answer" and then accepting it, which does not give credit to the volunteer who actually helped you. Please use comments for commenting.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 18 Dec 2018

2 Comments

Anton Fernando
Anton Fernando on 18 Dec 2018
Edited: Anton Fernando on 18 Dec 2018
I actually found multiple similar solutions. I am not a heavy matlab user. Therefore when the coding gets complicated it becomes difficult for me to figure out how to modify a given soultion for my particualr problem. The answer you posted does not explain very well with enough comments how to modify it for a different question. (Maybe it did, But the codes are really complcated). But thank you very much.
I figured out a way to save all the file names I need. Thanks. Now I only need to read data from individual files.

Sign in to comment.

Asked:

on 18 Dec 2018

Edited:

on 18 Dec 2018

Community Treasure Hunt

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

Start Hunting!